Personal Git cheating sheet

this is a git cheatsheet for personal reference

get info

1
2
3
4
$ git log # show commit logs
$ git branch # show local branches
$ git branch -a # show local and remote branches
$ git tag # show tag info, return null if there is no tag in current branch

manage branch

1
2
3
4
5
$ git checkout # show which branch are you at
$ git checkout -b <branch name> #create and switch to new branch
$ git push -u origin <branch name> #push the local branch to remote (local branch has to exist)
$ git push --delete origin <branch name>
$ git branch -d <branch name> # can not delete the branch you are currently on, 4 has to switch to another branch first

.gitignore

1
2
3
4
5
$ vim .gitignore
# in .gitignore
# *.rar # ignore all .rar files
# build/ # ignore directory build
# *ignore/ # ignore directory with tail "ignore"

To update gitignore remove/add untracked/ignored files

  • commit change
  • 1
    git rm -r --cached . # remove the file from the index (untrack all files)
  • add and commit again

tag

1
2
3
$ git tag <tagname> # set a tag for latest commit
$ git pull --tags # update tags only
$ git tag -d <tagname> # delete tag by name

store uncommitted work

1
2
3
4
$ git stash # put current work state into stash list
$ git stash list # list all Work in progress (WIP) in stash list
$ git stash apply stash@{n} # apply the n-th WIP on stash list
$ git reset HEAD --hard # reset head to latest commit and discard all local change and clear stash list

add/edit remote

1
2
git remote rename origin xxxx
git remote add origin url