Git

Some git notes.

基本配置

git config

# List configuration
git config -l
git config --global -l

# Config user's info
git config --global user.name "Mr.Blue"
git config --global user.email "silverhugh.77@gmail.com"

# Delete config
git config --global --unset user.username

# Config editor for git, use vim, nano or so on
git config --global core.editor "vim"
# True way to Commit
#   `git commit` will open an vim editor (set by command before)
#   write the commit message in it and `:x` to commit

See also: https://chris.beams.io/posts/git-commit/

git alias

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.us 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.ls 'log --oneline -20'

远程分支的那点事儿

如何添加一个远程分支

  • 常规操作, git remote add origin git@githubo.com:[username]/[reponame].git
    • github.com 这里可以做一点文章
    • 对ssh进行一些配置,可以实现不同仓库使用不同的私钥进行推送
  • 添加一个自己用git init --bare创建的仓库时
    • git remote add origin ssh://git@example.com:[port]/~/example.git

设置upstream

#  -u to set upstream, next time you can run only `git push` or `git pull`
git push -u origin master

强行覆盖

  • 本地覆盖远程:git push -f origin master
  • 远程覆盖本地
    1. git fetch --all
    2. git reset --hard origin/master

如何吃好后悔药

修改commit作者(author)信息和提交信息(commit message)

修改最后一次提交

  • git commit --amend --reset-author This will user your author information in git config -l.
  • git commit --amend --author="Mr.Blue <silverhugh.77@gmail.com>"
  • --no-edit will keep old commit message.
  • Without it, it will open an editor which is configured by git config core.editor=nvim

修改任意个提交

  • git rebase -i <commit-ID> Modify commits after <commit-ID>
    • or just git rebase -i --root from start.
  • It will open an editor. There are enough information in opened editor.
  • If you want to modify some commits, just change pick of that line to edit
    • Just remove that line if you want remove a commit.
  • Save and exit the editor.
  • Then your next step is to modify those chosen commits one by one.
    • Use git rebase --continue to go to next commit point
  • Treat them with the method showed in the previous part.

标签

创建标签

  • Lightweight tag: git tag <tag-name>
  • Signed tag (Recommend): git tag -a <tag-name> -m <message>
    • without -m will launch editor

删除标签

git tag -d <tag-name>

推送标签

git push origin <tag-name>

切换标签

git checkout tags/<tag_name>

删除被gitignore的文件

  • git rm <file> --cached
  • git rm -r <folder> --cached