Show Git Status

git status

git log

git show

Update Your Local Repo

# ignore all local changes
git reset --hard
git pull

# keep all local changes
git stash
git pull
git stash pop

Creating Multiple Pull Requests

# PR is based on branch, and is one-to-one relationship
# create multiple branches to isolate the PRs
git checkout -b <branch-name>
# do you jobs and commit

Update Your fork

# Add a upstream repo
git remote add upstream <upstream-repo>
git fetch upstream
git checkout master

# Make a clean fork and also a clean commit
# It's always a good idea to fetch the upstream first
git fetch upstream
# The `--rebase` option places your changes on top of the latest commit without merges.
# `git pull` is fetch + merge
git pull --rebase upstream master

# Merge your commits into one
# Undo all your commits with your file changes untouched
git reset --soft upstream/master
# Commit your single changes
git commit -a
# Check your changes, use `gitx` in Mac OS X or `gitk` in linux
git diff <branch 1> <branch 2>

#git fetch upstream
#git checkout master
###git merge upstream/master master
# Rewrite your master branch so that any commits of yours that
# aren't already in upstream/master are replayed on top of that
# other branch:
#git rebase upstream/master

Resolving conflicts

# bring up a GUI tool to resolve it
git mergetool

Fix .gitignore not working

git rm -r --cached .
git add .
git commit -m ".gitignore is now working"

# Or you can remove specific file like finder cache
git rm --cached .DS_Store
# then commit you changes

Clone specific branch

git clone -b branch-name remote-repo

View Git remote URL

git remote -v
git remote show origin

Branching

# show current branch
git branch

# create new branch
git branch branch-name

# show all branches
git branch -a

# show all remote branches
git branch -r

# switch between branches
git checkout branch-name

Update index with all changes

git add -A

Stash

# stash the changes
git stash

# list the stash
git stash list

# pop the top stash
git stash pop

Commit

  • to add a feature
  • to remove a bug
  • to refactor the source code
# Squashing commits, e.g. squashing previous two commits
git rebase -i HEAD~2

# show file changes history
git log -p <filename>

# amending commit message
git commit --amend
git commit --amend -m "New commit message"

# editing commit history
git rebase -i HEAD~2
## this will show the last 2 commit changes made in a editor
## delete the commit you want to delete and save the changes
git push -f origin master
## push the changes with force to master branch

# go back commit
git log
git push origin +7f6d03:master
## or
git reset 7f6d03 --hard
git push -f origin master

# Revert the commit we just created
git revert HEAD

# Reset to most recent commit but keep all working directory changes
git reset
# Reset to most recent commit and overwrite all working directory changes
git reset --hard

Duplicating a repository

$ git clone --mirror http://git.code.sf.net/p/catacombae/hfsexplorer
# Make a bare mirrored clone of the repository

$ cd hfsexplorer
$ git remote set-url --push origin https://github.com/githubutilities/hfsexplorer.git
# Set the push location to your mirror

$ git fetch -p origin
$ git push --mirror origin

Tagging Release

# add a tag
git tag -a v0.1.0 -m 'version 0.1.0'

# delete a tag
git tag -d v1.0

# list all tags
git tag -l

# push
git push origin --tags

Git Post Hooks

Revert changes to a specific file

git checkout HEAD -- <your-file-name>

Default push branch

Reference

Git Reset Tutor

Setup git in vps

Git Cheat Sheet by Github Education