Git

Some git notes.

Basic configuration

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

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 a remote branch

Normal command:

git remote add origin git@githubo.com:<username>/<reponame>.git

There is some thing we can do to github.com. By using ~/.ssh/conig we can use different private key for different repo:

SSH config:

Host <hostname>
    HostName github.com
    IdentityFile <path of private key>

Host *
    IdentityFile <path of default private key>

Commands:

git clone git@<hostname>:<username>/<reponame>.git
git remote add origin git@<hostname>:<username>/<reponame>.git

When you create create a custom git server by run:

git init --bare

You can add this repo by running:

git remote add origin ssh://git@<ip>:<port>/~/<path relative to $HOME>.git
git remote add origin ssh://git@<ip>:<port>/<path relative to />.git

Setting default upstream

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

Overwrite

Overwrite remote branch:

git push -f origin master

Overwrite local branch:

git fetch --all
git reset --hard origin/master

Pull submodules

git submodule update --init --recursive     # First time
git submodule update --recursive

Feel free to regret

Modify commit

Only modify latest commit:

# Add extra modification
git add .
git commit --amend

# --no-edit will keep old commit message and not open editor
# git config core.editor=nvim
git commit --amend --no-edit

# reset user.name and user.email depend on your configuration
# git config -l
git commit --amend --reset-author

# reset authour information directly
git commit --amend --author="Mr.Blue <silverhugh.77@gmail.com>"

Modify information of any commits:

# Modify commits AFTER `<commit-ID>`
git rebase -i <commit-ID>

# From the begining
git rebase -i --root

# The commands above will open an editor. Change `pick` to `edit` of that line
#   if you want to change that commit. Save and exit the editor `:x`.
# git config core.editor=vim

# The work flow then will be a loop of two steps.
#   1. Modify your commit
git add .
git commit --amend

#   2. Move to next `edit` commit
git rebase --continue

Git tags

Create a tag

Lightweight tag:

git tag <tag-name>

Signed tag (Recommend):

git tag -a <tag-name>
git tag -a <tag-name> -m <message>

Remove a tag

git tag -d <tag-name>

Push tag to remote

git push origin <tag-name>

Switch to a tag

git checkout tags/<tag_name>

Remove file from git

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

--cached will reserve the file in your disk.