Git misc Tips

來源:互聯網
上載者:User
How to checkout a specific version of one file?

 

git checkout <commit hash> <filepath in this commit>git checkout <commit hash> -- checkout all files in this commit

 

=======================================================================

 

How to diff two versions of one file?

 

git diff HEAD^ HEAD <filepath>git diff <start commit hash>..<end commit hash> <filepath>

 

=======================================================================

 

Merge CAUTION:

 

When merge one branch to another(E.g: merge branch A to branch master), git will ONLY CARE the conflict on one line. If both branch modified the same line, conflict occurs(default merge strategy). So, if branch A DELETED some lines in a file while these lines does exist in branch master, there will NO CONFLICT reported. The merged version is the version in branch A.

 

So, if one file will be modified by 2 guys. We should create 2 branches for these 2 guys. When they finished modifying, commit changes then use "git merge trunk b1 b2" to merge these 2 branches into trunk branch. This will combine all changes into trunk. We CAN'T apply 1 guy's change to trunk, then create another branch for another guy, finally merge new branch into trunk. This will cause problems just described above.

 

And, merge is not always correct. More times merge will fail, especially when we merge a lot of branches together. So, be careful for the output when "git merge". If output message shows failed which git tried all merge methods, we can't take this merge result. We should delete this branch and create new branch for merging again. This time try to merge less branches, no conflict branches together. The goal is to make "git merge" no error occurs. Conflict is correct and we can take care of it but merge can't failed.

 

=======================================================================

 

Commit will NOT auto add new files

 

git commit -a will NOT call git-add to add new files. git commit -a just commit modified files. So if have new files added, please use git add first, then git commit -a.

 

=======================================================================

 

How to check one file's log and How to show the affected files in one commit

 

git log <filepath> -- check this file's log separatelygit log --name-only -- show affected files in every commit

 

=======================================================================

 

How to rename and remove files

 

git mv <file original name> <file new name>git rm <file path>git rm -r <dir path> -- remove a directory

 

All these should be use "git commit" next to make changes effect.

 

=======================================================================

 

How to modify commit message?

 

1. If you wanna modify the latest commit's message, just:   git commit --amend -m "new message here..."2. If you wanna modify NOT the latest commit's message, just:   git rebase -i HEAD~5 -- list 5 commit in editor, then change the "pick" to "edit" in the commit line which you wanna modify   git commit --amend -m "new message"   git rebase --continue

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.