Git Step-by-Step guide

Source: Internet
Author: User
Tags i18n using git git mergetool

Transferred from: HTTPS://GITHUB.COM/KAIYE/KAIYE.GITHUB.COM/ISSUES/7

After mastering the basics of Git use, you may encounter some 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仓库地址>
2. View Origin Warehouse Information
# 以下三种方式均可git config get --remote.origin.urlgit remote -vgit remote show origin
3. Delete Origin Warehouse information
git remote 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 take the example of adding GitHub ssh keys, and be careful to replace the GitHub file name.

Note: If you are unfamiliar with the key mechanism, we recommend that you do not specify the-f parameter, directly using the default Id_rsa file name.

# 运行以下命令,一直回车,文件名可随意指定ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/github# 如果不是默认密钥 id_rsa ,则需要以下命令注册密钥文件,-K 参数将密钥存入 Mac Keychainssh-add -K ~/.ssh/github# 将 pub 公钥的内容粘贴到线上网站的后台cat ~/.ssh/github.pub# 测试 git ssh 是否连接成功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:

git loggit reset <版本号>

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

git reset 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.

1, 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).

# 使用当前分支 HEAD 版本,通常是冲突源文件的 <<<<<<< 标记部分,======= 的上方git checkout --ours <文件名># 使用合并分支版本,通常是源冲突文件的 >>>>>>> 标记部分git checkout --theirs <文件名># 标记为解决状态加入暂存区git add <文件名>
2. The most common method

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.

3, 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.
4. The most complex 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:

# 将当前分支的版本追加到从远程 pull 回来的节点之后git pull --rebase# 若发生冲突,则按以上其他方法进行解决,解决后继续git rebase --continue# 直到所有冲突得以解决,待项目最后上线前再执行git push origin# 若多次提交修改了同一文件,可能需要直接跳过后续提交,按提示操作即可git rebase --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:

# 查看 stash 队列中已暂存了多少 WIPgit stash list# 恢复上一次的 WIP 状态,并从队列中移除git stash pop# 添加当前 WIP,注意:未提交到版本库的文件会自动忽略,只要不运行 git clean -fd . 就不会丢失git stash# 恢复指定编号的 WIP,同时从队列中移除git stash pop [email protected]{num}# 恢复指定编号的 WIP,但不从队列中移除git stash apply [email 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.

git config --global alias.st ‘status --porcelain‘

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:

fatal: reference is not a tree: f869da471c5d8a185cd110bbe4842d6757b002f5Unable to checkout ‘f869da471c5d8a185cd110bbe4842d6757b002f5‘ in submodule path ‘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
    • Commonly used to play tag operation, more please see "Git basics-Play Tag"

      # 列出所有本地 taggit tag   # 本地新增一个 tag,推送至 origin 服务器git tag -a v1.0.0 -m ‘tag description‘git push origin v1.0.0# 删除本地与 origin taggit tag -d v1.0.0git push origin --delete v1.0.0
    • Using git GUI clients (such as Souretree, Github Desktop) can greatly improve the efficiency of branch management. A branch merge operation typically has only two cases: from Origin merge to local, and git pull from another local branch to the current branch, with the git merge <分支名> following common commands:

      # 新建分支 branch1,并切换过去git checkout -b branch1# 查看所有本地与远程分支git branch -a# 修改完成后,切换回 master 分支,将 branch1 分支合并进来git checkout mastergit merge branch1# 删除已完成合并的分支 branch1git branch -d branch1

Git Step-by-Step guide

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.