Most Used Git Operations

Posted by Shawn Duan on April 12, 2016

Some Git commands/behaviors most used in my daily work.

About config

# edit / for current repo
git config "username"
git config

git config --list	# list git config
git config --edit	# open git config for editing

Move commit(s) to other branch

  • Move to a new branch

git branch newbranch
git reset --hard HEAD~3 	# Go back 3 commits. You *will* lose uncommitted work.*1
git checkout newbranch
  • Move to an existing branch

git checkout existingbranch
git merge master
git checkout master
git reset --hard HEAD~3 	# Go back 3 commits. You *will* lose uncommitted work.
git checkout existingbranch

Create new branch

  • Switch to new branch

git checkout -b new branch
  • Stay in old branch

git branch new branch

Merge multiple commits into one

git rebase --interactive HEAD~2		# gonna merge last 2 commits

In the editor:

pick b76d157  b
pick a931ac7  c

# Rebase df23917..a931ac7 onto df23917
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.

change the latter pick to squash.

pick b76d157  b
squash a931ac7  c

If you want to merge more than 2 commits, change all pick to squash, except the first one.

Save and quit, you’ll get another editor whose contents are:

# This is a combination of 2 commits.
# The first commit's message is:


# This is the 2nd commit message:


You can rename the commits here if you want. Save and quit. Check git log to verify.

Change name of the last commit

git commit --amend

Remove local untracked files

git clean -f -n 	# Show what will be deleted with the -n option
git clean -f 		# Delete files
git clean -f -d 	# Delete directories
git clean -f -X 	# Delete ignored files
git clean -f -x 	# Delete ignored as well as non-ignored files

comments powered by Disqus