Git

  • git branch -vv check for remote repo connection

  • git branch -r/-a list remote/all branches

  • git branch -u origin/<branch> set the upstream branch for current branch

  • git blame <file-name> check for contributors

  • git cherry-pick <commit1> <commit2> ... pick commits and append to current HEAD

  • git config --global user.email "email@example.com"

  • git config --global user.name "Your Name"

  • git config --global alias.s status use git s in short for git status

  • git config --global push.default simple

  • git config --global pull.ff only

  • git config --global core.autocrlf true/false/input

  • git config --global --unset <entry-name> unset global configuration

  • git clone -b <branch-name> --single-branch <url>

  • git commit --amend re-commit the last commit

  • git checkout - go to previous branch

  • git checkout <branch>/<commit-id>/<tag>

  • git checkout -b <branch> create a new branch and checkout to it

  • git checkout -b <branch> origin/<branch> also specify the upstream branch

  • git checkout <file-name> undo a file to the last commit (valid before git add this file)

  • git clean <file-name> -f remove untracked file

  • git clean <directory-name> -df remove untracked directory

  • git clean -X -f remove files ignored by .gitignore

  • git diff --staged/cached diff between last commit and staged files

  • git diff <id1> <id2>

  • git diff <branch1> <branch2>

  • git describe --tags --abbrev=0 most recent tag in current branch

  • git log --graph --pretty=oneline --decorate --all

  • git push --force force update remote repo (overwrite commits in remote repo)

  • git push origin master will push local master to remote master, regardless of current branch

  • git push origin <source>:<destination> push local source branch to remote destination branch where source could also be a location represented by HEAD~1 or master^

  • git push origin :foo push nothing to a remote branch foo will delete this branch

  • git fetch origin :foo fetch nothing to a local branch foo will create this new branch

  • git pull = git fetch + git merge; two branches in history

  • git pull --rebase = git fetch + git rebase; a single line in history (recommended)

  • git rm <file-name> -f remove a staged file and delete it from file system

  • git rm <file-name> --cached remove a staged file but still keep it in working directory

  • git reset <file-name> move a file from staging to working directory

  • git reset --hard/--soft/--mixed <commit-id>

  • git remote -v to see the current remote URL

  • git remote set-url origin <newurl> reset URL for origin

  • git revert <commit-id> go back to this commit by adding a new commit

  • git reflog see all history operations

  • git rebase <branch> merge into a single line by making copies of new commits

  • git rebase -i interaction mode (can pick commits)

  • git stash

  • git stash list/pop/apply/drop/clear

  • git show <branch-name>:<file-name>

  • git status --ignored show files ignored by .gitignore

  • git tag <name>

  • git tag -ln check for detailed info

  • git update-index --assume-unchanged <path/to/file> untrack/hide a file

  • git update-index --no-assume-unchanged <path/to/file> track/unhide a file

references: Version Control with Git; Learn Git Branching; How to undo almost anything with git

Last updated