以前在公司内部推Git,写了一份git使用教程,后来又在团队内部作了一次分享,内容是关于Git使用过程当中常常会遇到的一些场景,并有了这份总结。git
基于feature的工做流github
更多详细查看上面教程连接shell
git remote add origin <your-repo-git-url>
复制代码
git commit --amend -m "your new log"
复制代码
git reflog
复制代码
git show <commit_id>
复制代码
git reset --hard HEAD
git checkout -- <file_name>
复制代码
git reset --mixed HEAD
复制代码
git reset --soft HEAD^
复制代码
git revert <commit_id>
git revert HEAD
复制代码
// 保存现场
git stash
// 恢复现场
git stash pop
复制代码
git checkout <commit_id> -- <file_name>
复制代码
另外你也能够用reset命令来完成bash
git cherry-pick <commit_id>
复制代码
好比:我想把如下分支工具
A-B master
\
C-D-E-F-G develop
复制代码
中的D,F 两次提交移动到master分支,而保持其余commit不变,结果就像这样学习
A-B-D-F master
\
C-E-G develop
复制代码
那么,思路是将D,F 用cherry-pick应用到master分支上,而后将develop分支对master分支变基。fetch
$ git checkout master
$ git cherry-pick D
$ git cherry-pick F
$ git checkout develop
$ git rebase master
复制代码
注意有些状况下使用cherry-pick会存在冲突,解决方法和咱们平时合并分支遇到冲突同样。网站
如:常见的拉取同事的代码合并引发冲突this
1. 手动处理冲突
2. 文件标志位置为resolved:git add <file_name>
3. 继续合并 git merge --continue
固然也能够选择放弃合并:git merge --abort
复制代码
好比咱们在bugfix分支上面因为修改bug提交了不少次,修复好了以后,咱们想把这些提交合并入咱们的master分支url
git checkout master
git merge --squash bugfix
git commit -m "bug fixed"
复制代码
上面操做会将bugfix分支上的全部commit都合并为一个commit,并把它并入咱们的master分支上去。这里还有一点须要注意的是:--squash含义表明的是本地内容与不使用该选项的合并结果相同,可是不提交,不移动HEAD指针,因此咱们要另外多一条语句来移动咱们的HEAD指针,即最后的commit。
git rebase -i <commit>
复制代码
举例:
git rebase -i HEAD~5
执行完后,Git会把全部commit列出来,让你进行一些修改,修改完成以后会根据你的修改来rebase。HEAD-5的意思是只修改最近的5个commit。
pick 033beb4 b1
pick b426a8a b2
pick c216era b3
pick d627c9a b4
pick e416c8b b5
# Rebase 033beb4..e416c8b onto 033beb4
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#
复制代码
上面pick是要执行的commit指令,另外还有reword、edit、squash、fixup、exec这5个,具体的含义能够看上面的注释解释,比较简单,这里就不说了。 咱们要合并就须要修改前面的pick指令:
pick 033beb4 b1
squash b426a8a b2
squash c216era b3
squash d627c9a b4
squash e416c8b b5
复制代码
也就是下面这4个提交合并到最前面的那个提交里面,按esc,打上:wq提交保存离开。 接着是输入新的commit message
b
# This is a combination of 2 commits.
# The first commit's message is:
# b1
#
# This is the 2nd commit message:
#
# b2
#
# This is the 3rd commit message:
#
# b3
#
# This is the 4th commit message:
#
# b4
#
# This is the 5th commit message:
#
# b5
#
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# Not currently on any branch.
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: a.txt
#
复制代码
其中第一行的b就是须要咱们输入的新信息,一样编辑完保存,出现相似下面的信息:
Successfully rebased and updated refs/heads/develop.
复制代码
最后能够用git log指令来验证commits是否是咱们要变成的样子。
一般查问题时想知道某个文件的某部分代码是谁改动的,那么git blame 就派上用场了。
git blame <file_name>
复制代码
你也能够具体指定到某一行或者某几行代码
git blame -L <start_line>,<end_line> <file_name>
复制代码
有时候会作代码备份,将代码保存在几个不一样的Git代码管理平台,这时候就须要用到了
修改本地仓库目录下.git/config文件
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = git@github.com:yuxingxin/blog.git
url = ……
url = ……
fetch = +refs/heads/*:refs/remotes/origin/*
复制代码
如上 在remote处能够添加多个远程地址。
# 开始 bisect
$ git bisect start
# 录入正确的 commit
$ git bisect good xxxxxx
# 录入出错的 commit
$ git bisect bad xxxxxx
# 而后 git 开始在出错的 commit 与正确的 commit 之间开始二分查找,这个过程当中你须要不断的验证你的应用是否正常
$ git bisect bad
$ git bisect good
$ git bisect good
...
# 直到定位到出错的 commit,退出 bisect
$ git bisect reset
复制代码
固然了,git的一些常见场景,还远不止这些,限于本人能力有限,若是你在平时的工做中遇到一些很实用的命令,也欢迎反馈给我,我好一并学习。更多的详细能够参考以前总结的一系列文档: devops.yuxingxin.com。 学习git命令是一件颇有意思的事情,我想它能帮助使用git命令的人更好的理解这一代码管理工具,从而不至于犯一些低级错误,MobDevGroup网站上面也分享过几个学习命令的网站,能够供参考:mobdevgroup.com/tools/assis…