做好git工作流

每天写代码前的第一件事:要对你的工作的branch做git rebase

具体步骤就是:

  1. 换回到master branch
  2. pull最新的master branch上的更新
  3. 切回到你工作的branch
  4. 然后在更新的master上做rebase
git checkout master
git pull
git checkout your-branch
git rebase master

如果不幸有冲突merge conflicts,那么我们需要额外三步。即第五步到第七步。第五步mergetool来查看冲突,第六步需要进入code进行修改,第七步继续rebase。

git mergetool
git rebase --continue

关于第一步mergetool,可以参照What’s the best visual merge tool for Git?。如何configure merge tool,可以用如下

git config --global merge.tool your_choice_of_mergetool

如果依然有merge conflicts,重复第五步到第七步。

最后如果你后悔了,想撤销刚才做的:

git rebase --abort

正确地commit

A best practice when using Git is to make sure each commit consists of only a single logical change. whether that’s a fix for a bug or a new feature. Sometimes when you’re working, however, you’ll end up with more than one commit’s worth of change in your repository. How can you manage to divide things up so that each commit contains only the appropriate changes? git add --patch to the rescue!

git add -p

创建新branch

首先在创建新的branch之前,需要保证master是最新的。因此pull the changes from远程的master,然后在本地创建新的branch,并转到该branch上。最后将新建的branch同步到远程,并将origin master设置为新的branch的upstream:

git pull
git checkout -b name_of_your_new_branch
git push --set-upstream origin name_of_your_new_branch
//确认是否远程有更新
git branch -a

删除本地changes

“git clean” removes all untracked files (warning: while it won’t delete ignored files mentioned directly in .gitignore, it may delete ignored files residing in folders

git checkout clears all unstaged changes.(即git add之前的那些changes)

git clean -df
git checkout -- .

那些已经git add的changes,可以这么做:

git reset file_name
或者直接
git reset

Leave a Comment