最近公司新来了个同事,大家都叫他老王,老王并不是职场老鸟,只是年长大家几岁。接下来讲讲4年工作经验的老刘和菜鸟老王不得不说的故事。
老王初入职场,对于git的使用并不熟练再加上PM朝令夕改所以最近频发牢骚。
stash篇
PM:老王,你上次上线的验证码功能得改改,这是原型图,手上其他的task先放放,优先处理这个。
老王:好的,那我先把这块未完成的代码提交上去。
老刘:公司规定未完成的代码不允许commit,每个commit要保证是完成了某个功能或fix了某个bug。
老王:那我怎么办啊? - - !
老刘:使用 git stash先放到堆栈中存起来
老王:git stash ? ? ?
此时老牛接过了电脑开始了一波操作:
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: Gemfile
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
$ git stash
jane➜project/git/homeland(master)» git stash [19:54:40]
Saved working directory and index state WIP on master: f2bec54 修复 Markdown 内容多出滚动条的问题 ref #1035
HEAD is now at f2bec54 修复 Markdown 内容多出滚动条的问题 ref #1035
jane➜project/git/homeland(master)» git status [19:55:01]
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
老刘:好了,你现在的分支干净了,可以开始写代码了。
老王:哇,好神奇,新技能get√。但是有个问题啊,我怎么再拿出来?
老刘:git stash apply
# 列出所有缓冲区
$ git stash list
stash@{0}: WIP on master: f2bec54 修复 Markdown 内容多出滚动条的问题 ref #1035
stash@{1}: WIP on master: 654f579 A add test gem to Gemfile
stash@{2}: WIP on master: f2bec54 修复 Markdown 内容多出滚动条的问题 ref #1035
stash@{3}: WIP on master: f2bec54 修复 Markdown 内容多出滚动条的问题 ref #1035
# 拿出最近储藏的文件
# git stash apply stash@{x}可以拿出索引为x的文件
jane➜project/git/homeland(master)» git stash apply [19:56:02]
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: Gemfile
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
commit篇
老王:老牛,我本地修改了一些文件,但是我只想提交其中一个,应该怎么办?
老刘:so easy!
jane➜project/git/homeland(master)» git status [19:26:38]
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: Gemfile
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
# git commit --only -m <commit message> -- <files to commit>
jane➜project/git/homeland(master)» git commit --only -m 'A add test gem to Gemfile' -- Gemfile
[master 654f579] A add test gem to Gemfile
1 file changed, 1 insertion(+)
jane➜project/git/homeland(master)» git status [19:28:08]
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
老王:新技能get√
reset篇
老王:我刚刚弄了一个错误的commit,我要如何回到该commit之前的代码?
老牛:so easy!
jane➜project/git/homeland(master)» git stash [19:30:53]
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working directory clean
# git reset --hard commitID # 回到commitID的版本
# git reset --soft commitID #删除commitID之后的commit记录,但是代码不变
git reset --hard f2bec549d4d6e4ea226ef8bc017adbe8c9560b89
老王:新技能get√
assume unchanged篇
老刘:老王,你这是怎么了?大早上的就愁眉苦脸了?
老王:我本地的配置文件和其他developer不一致,所以要在项目中的配置文件修改一些参数,这个文件不能提交,平时都是在提交前恢复到默认配置,但是昨天误提交到git上被老大骂了一顿。
老刘:哈哈!
jane➜project/git/homeland(master)» git status [19:46:45]
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: config/initializers/redis.rb
no changes added to commit (use "git add" and/or "git commit -a")
# git update-index --assume-unchanged <files>
# git update-index --no-assume-unchanged <files> #恢复
jane➜project/git/homeland(master)» git update-index --assume-unchanged config/initializers/redis.rb
jane➜project/git/homeland(master)» git status [19:53:57]
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
老王:新技能get√
cherry-pick
老王:老刘,我想把v2分支中的一些commit放到v3分支里面来,git有提供好的方案吗?
老刘:使用git cherry-pick <commit id>就行啦!注意:当执行完 cherry-pick 以后,将会 生成一个新的提交;这个新的提交的哈希值和原来的不同,但标识名 一样。
老王:新技能get√
网友评论