Git advanced Guide (git ssh keys/reset/rebase/alias/submodule)

Source: Internet
Author: User
Tags i18n git mergetool

After mastering the basics of Git use, you may encounter some common problems. The following are some of the problems that cat Brother screening summary, share to friends, master the points in these questions, Git advanced also completed, it contains the following sections:

    • How to modify Origin warehouse information
    • How to configure Git ssh keys
    • How to undo Changes
    • How to solve the conflict
    • Git stash/alias/submodule usage issues, etc.
Q: How do I modify the Origin warehouse information? 1. Add Origin Warehouse Information
Git remote add Origin <git warehouse address >

2. View Origin Warehouse Information
--vgit remote show Origin

3. Delete Origin Warehouse information
RM Origin

Q: How do I configure git ssh keys?
    1. Generate an SSH private key/public key file locally
    2. Add a "public key" to the Git Service (GitHub, Gitlab, coding.net, etc.) website backstage
    3. Test if the Git SSH connection is successful

Next, for example, add GitHub ssh keys, and note that the GitHub file name is replaced:

# Run the following command, always enter, the file name can be arbitrarily specified Ssh-keygen 4096 " [email protected] " -f ~/. ssh/github# If it is not the default key Id_rsa, you need the following command to register the key file ssh-add ~/. ssh/github# Pastes the contents of the pub public key into the background cat ~/of the online website. ssh/ssh  connection successful ssh -t [email protected]

Q: How do I undo my changes?

The modification consists of four cases, which need to be differentiated separately.

1. New files and directories, and never submitted to Repository

The status of this type of file is Untracked files , the revocation method is as follows:

Git clean-fd.

Where the . file is represented in the current directory and in all subdirectories, and the corresponding file path can be specified directly, similar to the following other cases.

2. Files submitted to the repository but not submitted to staging area (git add not executed)

The status of this type of file is Changes not staged for commit , revocation method:

Git checkout.

3. Documents submitted to Staging area

The status of this type of file is Changes to be committed , revocation method:

git reset.

After executing the file will return to the above 1 or 2 status, you can continue to follow the above steps to undo, if git reset plus --hard parameters, will be the modified files are also restored to the version of the repository.

4, submitted to the repository (git commit executed)

Each commit generates a hash version number that can be consulted and rolled back with the following command:

< version number >

If you need to "roll back the previous commit," You can use the following command directly:

Head~1

After execution, the process can be processed in 1 or 2, and if the post-rollback code needs to be submitted to the Origin repository at the same time (that is, the code that rolls back the warehouse on the Origin line), the -f mandatory commit parameter is required and the current user needs to have the Force commit permission.

5. What if I roll back and don't want to roll back?

If this is the case 1 or 2, only fart, because the changes in the repository, can not be rolled back.

If this is the case 4, the rollback by git log will not see the version number before rollback, but git reflog the version number before the rollback can be found by command (all used version number), and then git reset <版本号> .

Q: How to solve the conflict?

When two branches are merged (usually git pull), they may encounter conflicts, while the modified files enter the Unmerged state and need to resolve the conflict.

The quickest way.

Most of the time, the quickest way to resolve conflicts is to use the current HEAD version (ours), or use the merged branch version (theirs).

# using the current branch HEAD version, usually the <<<<<<< tag portion of the conflicting source file, =======--ours <file name > >>> >>>>--theirs < file name> < filename >

The most common way

Using the editor to open conflicting source files for modification can be a legacy and a bad experience, usually with the help of the git mergetool command.

Under the MAC system, run the git mergetool <文件名> merge with a third-party tool that can turn on the configuration, default is the Filemerge application, and can be configured as Meld or KDIFF3 for better experience.

The best habit

There are three good habits that can reduce the conflict of code:

    • Before you start modifying the code git pull ;
    • Division of Business code, as far as possible, not many people in the same time period to modify the same file;
    • The Gitflow workflow also increases the efficiency of the GIT process and reduces the likelihood of conflict.
The most complicated situation

If you have a long project cycle, you should also develop a "regular rebase" that allows you to git pull --rebase keep your branch code compatible with the code in your origin repository without disrupting the reliability of your online code.

The approximate principle is that the Code of Origin warehouse is submitted in the local branch by the time stream of origin, and then the modification record of the local branch is appended to the Origin branch. In the event of a conflict, the problem can be identified and resolved immediately, or additional risk may occur when the project is brought online to resolve the conflict.

Rebase approximate operation steps are as follows:

-----skip

Q: How do I perform operations such as Pull/merge without committing changes?

Some modifications are not fully completed and may not need to be submitted to the repository, the 圡 method is to copy the modified files to a directory other than the GIT repository for temporary storage, and then copy them back after the pull/merge operation is complete.

One such approach is inefficient, and the other may miss out on potential conflicts. Such requirements are best accomplished by git stash command, which temporarily stores the current working state (Wip,work in progress) on the stash queue, and then reapply those modifications from the stash queue after the operation is complete.

The following are common commands for git stash:

-FD. Will not lose git stash# restore the specified number of WIP, while removing git stash pop from the queue [email protected]{num}# recover the WIP of the specified number, but do not remove git from the queue stash apply [E Mail Protected]{num}

Q: How do I view a list of modified files in git log?

The default git log displays a more complete message and does not contain a list of files. Use the --name-status list of files that you can see modified to simplify the parameters to a single --oneline line.

git log--name-status--oneline

Each time you manually add a parameter is cumbersome, you can simplify the operation by customizing the shortcut commands:

git config--global alias. ls ' log--name-status--oneline--graph '

After running the above configuration, you can use git ls commands to implement the custom Git log"effect, or you can create git st and git ci wait a series of commands to follow the SVN command-line habit.

More git log parameters are available by git help log viewing the manual.

If you are interested in a committed version log, run it directly git show .

Q: How do I fix an error when git submodule update?

For example, you have the following error message when you perform a git submodule update:

' f869da471c5d8a185cd110bbe4842d6757b002f5 ' inch ' Source/i18n-php-server '

In this case, the above error occurs because the I18n-php-server sub-warehouse has a new version "f869da471c5d8a185cd110bbe4842d6757b002f5" in the local "commit of a computer A, and the commit is not push Origin However, the version number of the sub-warehouse is referenced in its parent warehouse i18n-www, and the reference record is push origin, causing other clients to fail to update.

Workaround, perform a git submodule update on other clients after the I18n-php-server repository is push origin on computer A. Or, using the Git reset method mentioned above, restore the reference version number of the sub-repository to the latest version number that exists on Origin.

Other questions
    • Set the local branch to stay in sync with the remote branch, with parameters on the first git push -u

       
    • Support for the display of Chinese directories and filenames (git displays non-ASCII encoded directories and filenames in octal code by default)

      git config core.quotepath off

If you have other questions, please leave a message on GitHub to add:)

Git advanced Guide (git ssh keys/reset/rebase/alias/submodule)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.