Skip to content

Instantly share code, notes, and snippets.

@GeekEast
Created February 28, 2021 02:53
Show Gist options
  • Save GeekEast/214d51231f4bc615744d9afcccb35409 to your computer and use it in GitHub Desktop.
Save GeekEast/214d51231f4bc615744d9afcccb35409 to your computer and use it in GitHub Desktop.
Practical Git Commands
# init
git init                           # init a repo
git clone <remote_url>             # clone a repo from remote

# commit
git add <filename>                 # add to index/staging area
git add .                          # add all files to staging area
git rm  <filename>                 # remove file from index/staging area
git commit -m "commit message"     # snapshot!
git commit --amend                 # interactively change commit message if nothing in staging
git commit --amend                 # combine staging into last commit if staging is not empty

# remote
git remote -v                      # inspect remote repo
git remote add origin <remote_url> # link to remote repo
git remote rm origin               # unlink remote repo
git remote rename origin original  # rename shortcut for remote repo

# push
git push                        # push current branch to remote branch
git push origin --all           # push all branches to remote branches
git push -u origin main         # same as below if local branch name is same as remote branch name
git push -u origin main:main    # push local branch (left) to remote branch (right
git push -f                     # push to remote even if it results in a non-fast-forward merge in remote branch
git push origin --delete main   # delete remote main branch
git push origin :main           # delete remote mian branch
git push origin --tags          # push local tags to remote

# pull
git pull                        # pull changes from remote repo (all branches)
git pull origin main:main       # merge remote main (left) to local main(right)       
git pull origin main            # equals to above if local branch name and remote branch name is same
git pull --merge                # merge mode
git pull --rebase               # rebase mode

# fetch (pull requivalent)
git fetch                       # pull changes from remote repo (all branches), used to sync with remote server
git fetch --prune               # pull changes from remote repo (all branches and clear deleted branches)
git fetch origin origin/main    # only fetch the main branch
git merge origin/main           # merge fetched branch to current branch
git rebase origin/main          # rebase fectehed branch to current branch
  
# branch
git branch                     # list local branches
git branch -r                  # list remote branches
git branch -a                  # list local and remote branches
git branch -d <branch_name>    # remove branch
git branch -D <branch_name>    # (force) remove branch
git branch -u origin/main      # track current branch to remote main branch
git branch -m oldName newName  # rename branch name

# checkout (branch)
git checkout -b <branch_name>                    # create a new branch
git checkout -b localName origin/remoteName      # create a new branch and track remove branch
git checkout <branch_name>                       # switch to another branch

# checkout (commit) 
git checkout <commit_hash>                       # checkout to a commit on a temporary branch
git checkout .                                   # reset all files in current repo to HEAD **before** add
git checkout -- .                                # reset all files in current repo to HEAD before add
git checkout -- <filename>                       # you want to reset a file before add it to staging
git checkout HEAD~ -- <filename>                 # reset the file from the commit before HEAD

# rebase 
git checkout feature           # you should be at the feature branch at first
git rebase main                # git rebase <base>
---------------------         # iterate the below 2 steps until you got no conflicts
git add <filename>             # solve conflicts -> add to staging
git rebase --contine           # tell git to solve next conflicts
---------------------         # move HEAD to the last commit
git checkout main
git merge feature

# squash commits by rebase
git rebase -i HEAD~4           # you want squash last 4 commits (from bottom to up to change to squash)

# reset (dangerous: modify the history)
git reset                      # unstage all files **after** you do git add
git reset HEAD <filename>      
git reset --hard               # abandon changes in working directory and staging area
git reset --soft HEAD~3        # rest head to 3 and keep all current changes (you need to add & commit)
git reset --mixed              # keep changes in working directroy but abandon changes in staging area
git reset --hard HEAD~         # delete the last commi
git reset --hard HEAD~2        # delete the last 2 commits

# revert (safe: doesn't modify history, only create)
git revert <commit>            # create a new commit which redo the changes in <commit>

# cherry pick
git cherry-pick <commitHash>                     # apply commit
git cherry-pick <branch_name>                    # apply last commit of another branch to current branch
git cherry-pick <commitHash> <commitHash>        # multiple 
git cherry-pick A..B                             # serial, A is before B in time
git cherry-pick A^..B                            # include A

# stash (you must have at least 1 commit)
git stash                      # save current change to a box
git stash apply                # apply box changes back to current branch
git stash pop                  # apply and remove the stash box
git stash list                 # list all stash boxes
git stash drop <stash_name>    # remove one stash box
git stash clear                # remove all stash boxes
git add . & git stash & git stash drop     # undo all uncommited changes **after** add

# log
git log --graph                # show log in graph
git log --oneline              # condense each commit into one line

# HEAD history
git reflog --relative-date

# staus
git status

# compare
git diff                        # compare working area to staging area, show untracked content
git diff HEAD                   # compare working area to last commit
git diff --cached               # compare staging aread to last commit

# config
git config --list --local                                         # list local configs
git config --list --global                                        # list global configs
git config --list                                                 # list local and global configs
git config --local user.name "Xiang Tan"                          # set author name for commits in current repo
git config --global user.name "James Tan"                         # set author name for commits in all repos
git config --global user.email "[email protected]"         # set author email for commits in all repos
git config --global alias.glog "log --graph"                      # set alias for git log --graph
git config --system core.editor "code --wait"                     # set vscode as default editor for git, no vim anymore..
git config --global --edit                                        # edit the global configs in default editor
git config --global push.default current                          # automatically set up-stream branch when you push
git config pull.rebase true                                       # git pull --rebase by default

# decache
git rm --cached .DS_Store          # remove the cache of .DS_Store
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment