10 个很有用的高级 Git 命令 已翻译 100%

oschina 投递于 2013/08/22 07:48 (共 5 段, 翻译完成于 08-22)
阅读 15245
收藏 463
Git
40
加载中

I have been using git for quite some time now and thought of sharing some advanced git commands that you may find useful whether you are working in a team environment or on your personal project.

1. Export changes done in last commit

This command i have been using regularly for sending the changes done to another person for review/integration who is not on git. It will export the recent committed changed files to a zip file.

git archive -o ../updated.zip HEAD $(git diff --name-only HEAD^)

2. Export changed files between two commits

Similarly if you need to export changed files between two commits, you can use this one.

git archive -o ../latest.zip NEW_COMMIT_ID_HERE $(git diff --name-only OLD_COMMIT_ID_HERE NEW_COMMIT_ID_HERE) 

已有 1 人翻译此段
我来翻译

3. Clone a specific remote branch

If you wish to clone only specific branch from a remote repository without having to clone whole of the repository branches, this will be useful to you.

git init
git remote add -t BRANCH_NAME_HERE -f origin REMOTE_REPO_URL_PATH_HERE
git checkout BRANCH_NAME_HERE

4. Apply patch from Unrelated local repository

If you need to apply a patch from a commit on some other unrelated local repository to your current repository, here is a shortcut way to do that

git --git-dir=PATH_TO_OTHER_REPOSITORY_HERE/.git format-patch -k -1 --stdout COMMIT_HASH_ID_HERE| git am -3 -k

5. Check if your Branch changes are part of Other branch

cherrycommand lets you check whether your branch’s changes are present in some other branch or not. It will display the changes on current branch to given branch and indicate with a + or – sign to indicate if that commit is merged or not. + indicated not present while – indicates present in the given branch. Here is how to do that:

git cherry -v OTHER_BRANCH_NAME_HERE
#For example: to check with master branch
git cherry -v master

已有 1 人翻译此段
我来翻译

6. Start a new Branch with No History

Sometimes you need to start a new branch and do no want to carry the long history along, for example, you want to place the code in public domain(open source) but do no want to share the history.

git checkout --orphan NEW_BRANCH_NAME_HERE

7. Checkout File from Other Branch without Switching Branches

Here is how to fetch just that file you need from other branch without even have to switch branches.

git checkout BRANCH_NAME_HERE -- PATH_TO_FILE_IN_BRANCH_HERE 

已有 1 人翻译此段
我来翻译

8. Ignore Changes in a Tracked File

If you are working in a team and all of them are working on same branch, chances are you are going to use fetch/merge quite often. but this sometimes resets your environment specific config files which you have to change every time after merge. Using this command, you can ask git to ignore the changes to specific file. So next time you do merge, this file won’t be changed on your system.

git update-index --assume-unchanged PATH_TO_FILE_HERE

9. Check if committed changes are part of a release

The name-rev command can tell you the position of a committ with respect to a last release. Using this you can check if your changes were part of the release or not.

git name-rev --name-only COMMIT_HASH_HERE

已有 1 人翻译此段
我来翻译

10. Pull with rebase instead of merge

If you are working in a team which is working on same branch, then you have to do fetch/merge or pull quite often. Branch merges in git are recorded with merge commit to indicate when a feature branch was merged with mainstream. But in the scenario of multiple team members working on same branch, the regular merge causes multiple merge messages in the log causing confusion. So you can use rebase with pull to keep the history clear of useless merge messages.

git pull --rebase

Also, you can configure a particular branche to always rebasing:

git config branch.BRANCH_NAME_HERE.rebase true
已有 1 人翻译此段
我来翻译
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接。
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。
加载中

评论(27)

万事通
万事通
收藏了,支持楼主。
翅膀的初衷
翅膀的初衷
GIT一直不太熟,转走慢慢看
殊麒
殊麒

引用来自“zoogar”的评论

引用来自“殊麒”的评论

配图亮了

没看见配图

我用手机看的,有个憨豆先生的配图
zoogar
zoogar

引用来自“殊麒”的评论

配图亮了

没看见配图
zoogar
zoogar

引用来自“谁又谁”的评论

git基础命令哪里找?man?

man git
git --help
git help <sub-command>
IT过客
IT过客
不错,收藏下
hawkyoung
hawkyoung

引用来自“Lesus”的评论

引用来自“hawkyoung”的评论

3568 我觉得有帮助 不过对于8,不是用git ignore文件吗?

gitignore文件规定的是被git忽略的,即不用提交到库的东西。

第8条是不一样的,它是需要提交库中的,只是在(fetch和merge)即pull操作时,不会从库中拉下来,这样有利于在编程时在某个文件中设置自己的临时参数,而不用与其它人的一样,否则又得修改回自己的。

谢谢! :-D
八木
八木
mark
Honghe
Honghe
怎么没出处??
王者无双
配图亮了,mark
返回顶部
顶部