Memo - Git

This is one of my memos about using Git.

Memos are used for recording the problems I encountered during programming and its solutions. I also write down something that is hard to remember for myself.

References

Settings

Basic

# Config user's info
git config --global user.name "anxiao"
git config --global user.email "hugh_77@icloud.com"
# Delete config
git config --global --unset user.username
# List configuration
git config -l
git config --global -l

# Config editor for git, use vim, nano or so on
git config --global core.editor "vim"
# True way to Commit
#   the command below will open an vim editor (set by command before)
#   write the commit message in it and :wq to commit
#   See also: https://chris.beams.io/posts/git-commit/
git commit -a
#   or
git add SOME_FILES
git commit

# Remote origin
git remote add origin ssh://git#example.com:[port]/~/example.git
#  -u to set upstream, next time you can run only `git push` or `git pull`
git push -u origin master

SSH setting

See it from Memo - Shell

Alias

技巧和窍门

GUI: gitk

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'

Undo

# Directly add file(s) to last commit without change commit message
git commit -am "initial commit"
git add forgotten_file
git commit --amend

# Undo git add
#   "--" is used to disambiguate checkout branch or files
#   See also: https://git-scm.com/docs/git-checkout#_argument_disambiguation
git reset HEAD -- file_already_add

Branch

Show remote

git remote show origin

Pull all branches

mkdir some_dir
cd some_dir
git init
git remote add some_remote_origin
git fetch --all
for remote in `git branch -r`; do git branch --track ${remote#origin/} $remote; done
git checout target_branch

Create

git branch new_branch_name
git checkout new_branch_name
# The command below has the same effect with the commands above
git checkout -b new_branch_name

Delete

# Delete branch after you merge it
git branch -d branch_name

No track

# Keep file in a Git repo, but don't track changes
git update-index --assume-unchanged FILE_NAME  
git update-index --no-assume-unchanged FILE_NAME

Errors

# fatal: refusing to merge unrelated histories
--allow-unrelated-histories

Categories: ,

Updated:

Leave a Comment

Your email address will not be published. Required fields are marked *

Loading...