Skip to content

Instantly share code, notes, and snippets.

@deskoh
Last active January 22, 2024 07:50
Show Gist options
  • Save deskoh/d5248fb9d7db87459af8330d0f21ed0f to your computer and use it in GitHub Desktop.
Save deskoh/d5248fb9d7db87459af8330d0f21ed0f to your computer and use it in GitHub Desktop.
GIT Cheatsheet

Local Branching

# Checkout new branch
:master> git checkout -b new-feature

# Feature completed.

# Update local master branch
:new-feather> git checkout master
:master> git pull
:master> git checkout new-feature

# Rebase with master
:new-feature> git rebase master

# Merge feature branch to master
:new-feature> git checkout master
:master> git merge new-feature

# Push merged master
:master> git push [origin master]

# Delete local feature branch
:master> git branch -d new-feature

Public Branching

# Checkout new branch
git checkout -b new-feature

# Local commits or rebase...
# Never rebase commits once they've been pushed to a public repo!

# Push feature branch to remote
git push -u origin new-feature

# Subsequent local commits and push
git push

# Update local master branch
git checkout master
git pull

# Merge remote feature branch into local master
git pull origin new-feature
git push [origin master]

GIT Cheatsheet

Setup

Global config:

git config --global core.autocrlf input
git config --global pull.rebase true

# Configure local branch to always use --rebase (modifies .git/config)
git config branch.<name>.rebase true

# Configure every new branch to always use --rebase
git config branch.autosetuprebase always

Using .gitconfig file:

[pull]
	rebase = true

Undo local commits

# Undo all local commits without unstaging changes  (soft reset)
git reset --soft origin/master
# Undo last commit without unstaging changes (soft reset)
$ git reset HEAD~1 --soft
# Undo last commit and undo staged changes (mixed reset)
$ git reset HEAD~1

Add changes to previous commit / Update commit message

# Stage changes to add to previous commit (no change in commit message)
$ git commit --amend --no-edit
# Stage changes to add to previous commit and update commit message
# If no changes staged, only commit message is changed.
$ git commit --amend -m "New commit message"

Squash local commits before push

# Option 1: Undo all local commits and re-commit all changes in one commit.
$ git reset origin/master --soft
$ git add .
$ git commit -m "Commit message"

# Option 2: Selective squashing by choosing which commits to squash in interactive mode.
git rebase -i origin/master

Undo staged changes (i.e. git add)

git reset

Stash working state as branch

$ git checkout master

# remember where the master was referencing to
$ git branch previous_master

# Reset master back to origin/master
$ git reset --hard origin/master

Reset / Discard uncommited and staged changes

# Will result in current branch behind origin/master.
git reset --hard [HEAD]

# Rewrite branch history (for commits not pushed to origin yet)
git reset --hard <commit hash>

# Revert to commits behind origin, not recommended for branch shared, unlikely to use
git reset --hard <previous commit hash pushed to origin>

Rollback to a particular commit

# Will result in detached head state
# Can be run on sub-tree to only revert current sub-tree
git checkout <commit hash>

# To work on current commit (detached head state) in a new branch for subsequent commit
git checkout -b <branch_name>

# Equivalently
git checkout -b <new_branch_name> <commit hash>

git reset

  • Should not used for commits pushed to origin.
  • Moves both HEAD and Branch refs (won't result in detached HEAD)

MIXED

# Commit refs moved, staged changes undone and moved to working directory.
$ git reset
$ git reset --mixed HEAD
  • Used to only undo staged changes (if reset to HEAD)
  • Used for undoing commits and staged changes (if reset to earlier commit)

HARD

# Commit refs moved, staged changes undone, working directory reset.
$ git reset --hard
  • Used to reset everything to specified commit.

SOFT

# Only commit refs moved only. (No effect if reset to `HEAD`)
$ git reset --soft
$ git reset --soft HEAD

# Only commit refs moved only.
# Staged changes combined such that committing them will result in same state before reset.
$ git reset --soft <commit hash>
  • Used for undoing commits (if reset to earlier commit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment