Tips when using Git

Posted by Jianing on February 1, 2020

Git

Good practice

  • Merge branches on Github instead of local repo, so that everyone can view this record easily.
  • Delete the merged branch.
  • After deleting a branch locally, its remote is still tracked.
1
2
git branch -d <name>
git branch -p

Better pull request workflow

  • sync with origin repo
  • work on a new branch dev
  • create a PR on the new branch
  • after PR is merged, pull it to master branch
  • delete the dev branch

Always get changes directly from origin, so the commit flow will be exactly the same as the upstream.

Rebase/Reset techniques

Undo some commits

1
2
3
git reset --hard HEAD^ <filepathname>   // 回退某个文件到上一次commit的状态
git reset --hard HEAD^    // 回退所有文件到上一次commit的状态
git reset --hard  <commit id>   // 回退所有文件到某个commit

rebase: put all local change (always on a dev branch)’into a patch; rebase to target (usually master); apply all modification in the patch to the current branch.

1
2
git rebase <base>   # 将<base>做为当前分支的新起点, <base>可以是任何一种commit引用(如ID,branch name,tag,HEAD~N等)。
git rebase -i <base>    # 交互式地将<base>做为当前分支的新起点,过程中可以对要rebase的commit做一定的修改。

Issues

  • Windows(rn)、Linux(n)和MacOS(r)三个主流系统的换行符各不相同,这样在跨平台合作的时候就容易出现换行符的问题。解决方案
1
2
3
4
5
# 签出时将换行符转换成CRLF,签入时转换回 LF。
 git config --global core.autocrlf true  

#签出时不转换换行符,签入时转换回 LF 
git config --global core.autocrlf input