10 个很有用的高级 Git 命令

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) 
译者信息

迄今,我已经使用Git很长一段时间了,考虑分享一些不管你是团队开发还是个人项目,都受用的高级git命令。

1. 输出最后一次提交的改变

这个命令,我经常使用它 来发送其他没有使用git的人来检查或者集成所修改的。它会输出最近提交的修改内容到一个zip文件中。

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

2. 输出两个提交间的改变

类似的,如果你需要输出某两个提交间的改变时,你可以使用这个。

git archive -o ../latest.zip NEW_COMMIT_ID_HERE $(git diff --name-only OLD_COMMIT_ID_HERE NEW_COMMIT_ID_HERE) 
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
译者信息
3. 克隆 指定的远程分支

如果你渴望只克隆远程仓库的一个指定分支,而不是整个仓库分支,这对你帮助很大。

git init
git remote add -t BRANCH_NAME_HERE -f origin REMOTE_REPO_URL_PATH_HERE
git checkout BRANCH_NAME_HERE
4. 应用 从不相关的本地仓库来的补丁

如果你需要其它一些不相关的本地仓库作为你现在仓库的补丁,这里就是通往那里的捷径。

git --git-dir=PATH_TO_OTHER_REPOSITORY_HERE/.git format-patch -k -1 --stdout COMMIT_HASH_ID_HERE| git am -3 -k
5. 检测 你的分支的改变是否为其它分支的一部分

cherry命令让我们检测你的分支的改变是否出现在其它一些分支中。它通过+或者-符号来显示从当前分支与所给的分支之间的改变:是否合并了(merged)。.+ 指示没有出现在所给分支中,反之,- 就表示出现在了所给的分支中了。这里就是如何去检测:

git cherry -v OTHER_BRANCH_NAME_HERE
#例如: 检测master分支
git cherry -v master
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 
译者信息
6.开始一个无历史的新分支

有时,你需要开始一个新分支,但是又不想把很长很长的历史记录带进来,例如,你想在公众区域(开源)放置你的代码,但是又不想别人知道它的历史记录。

git checkout --orphan NEW_BRANCH_NAME_HERE
7. 无切换分支的从其它分支Checkout文件

不想切换分支,但是又想从其它分支中获得你需要的文件:

git checkout BRANCH_NAME_HERE -- PATH_TO_FILE_IN_BRANCH_HERE 
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
译者信息

8.忽略已追踪文件的变动

如果您正在一个团队中工作,而且大家都在同一条branch上面工作,那么您很有可能会经常用到fetch和merge。但是有时候这样会重置您的环境配置文件,如此的话,您就得在每次merge后修改它。使用这一命令,您就能要求git忽视指定文件的变动。这样,下回你再merge的话,这个文件就不会被修改了。

git update-index --assume-unchanged PATH_TO_FILE_HERE

9.检查提交的变动是否是release的一部分

name-rev命令能告诉您一个commit相对于最近一次release的位置。使用这条命令,您就可以检查您所做出的改动是否是release的一部分了。

git name-rev --name-only COMMIT_HASH_HERE
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
译者信息

10.使用rebase推送而非merge

如果您正在团队中工作并且整个团队都在同一条branch上面工作,那么您就得经常地进行fetch/merge或者pull。Git中,分支的合并以所提交的merge来记录,以此表明一条feature分支何时与主分支合并。但是在多团队成员共同工作于一条branch的情形中,常规的merge会导致log中出现多条消息,从而产生混淆。因此,您可以在pull的时候使用rebase,以此来减少无用的merge消息,从而保持历史记录的清晰。

git pull --rebase

您也可以将某条branch配置为总是使用rebase推送:

git config branch.BRANCH_NAME_HERE.rebase true

转载【http://www.oschina.net/translate/10-useful-advanced-git-commands?cmp】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值