1. git stash
We sometimes encounter such a situation. We are developing halfway on branch a, and then a bug is found on branch b, which needs to be dealt with immediately. At this time, what should I do with the modification on branch a? It git add
is not acceptable. Some git client versions will prompt that the added files have not been submitted and cannot switch branches. Some git client versions will bring the modifications to the b branch.
git stash
It is to solve this problem, it saves the modification and content of the current workspace in git add
one place, and then git reset HEAD
returns the workspace to the last commit, in a clean state. Then you can safely cut to another branch b to work.
# 1.保存当前工作环境保存
git stash save "先给我保存一下,我要去别的分支修bug"
# 2.切换到b分支处理bug,处理完后提交
git checkout b
....
# 3.切换到a分支,并还原初始工作环境
git checkout a
git stash list
git stash pop
# git stash pop相当于执行git stash apply和git stash drop
# git stash apply stash@{num}
2. git rebase
Sometimes when we are developing in a branch a, the master has already entered a lot of modifications. If the modification of a is submitted at this time, it may conflict with the trunk, and it is necessary to resolve the conflict in the trunk before committing, which is rather ugly.
This is useful at this time git rebase
, git rebase BRANCH_NAME
you can bring the modification of the BRANCH_NAME branch to the current branch, so that the current branch has all the contents of the BRANCH_NAME branch, so that the content developed in the current branch will not conflict with BRANCH_NAME after the submission, the conflict is in the current branch can be solved.
3. git reset
You can cancel the commit that has been submitted, generally we only use it git reset HEAD^
. Because each branch may have many commits in the development process in order to save the process for backtracking, but we require that when entering the trunk, each function and bugfix can only have one commit, so you can use it to go git reset
back to the earliest commit first, and then put yourself The modifications are finally packaged into a commit, and then merged with the trunk.
Using these two commands, we can manage our project development very well. We only have one master branch as the trunk, and direct development on the trunk is not allowed. Each student establishes a branch according to the feature and bug issue, and then develops on the branch. No matter how many commits there are in the development process, we require that each bugfix or feature can only be submitted with one commit. Therefore, after each student completes the development, they need to go git reset
back to the earliest commit, git stash save
save their own changes, and then git checkout master
; git pull
drag the latest trunk, then return to their own branch, do it again git rebase master
, push the trunk branch to the current branch, and finally git stash pop
pop up the modification , if there is a conflict, it will be resolved in the current branch, and then git push
.
# 1.修改bug1234
git checkout -b bug1234
# 2.在bug1234分支上进行修改,并可能多次git commit
# 3.开发完成后,要提交
git reset a37b9ff
git stash save "fix bug 1234"
git checkout master
git pull
git checkout bug1234
git rebase master
git stash pop
# 处理冲突...
git add .
git commit
git push