Dec 14th, 2015

Submitting a PR from a specific branch

To resync your master with all the changes.

$ git fetch upstream
$ git checkout master
$ git merge upstream/master
$ git push origin master

If there is no upstream

$ git remote -v
> origin (fetch)
> origin (push)
$ git remote add upstream
$ git remote -v
> origin (fetch)
> origin (push)
> upstream (fetch)
> upstream (push)

Now your master has the latest code. Create a new branch called ISSUE1 now and you can fix the bug and submit a PR from that branch.

To list branches

$ git branch -a

Create a new branch named ISSUE1

$ git branch ISSUE1

Switch over to the branch ISSUE1 when you want to add new commits to it.

$ git checkout ISSUE1

Once you are on the ISSUE1 branch, you can start adding commits to it.

$ git add .
$ git commit -m "commit for ISSUE1"

To merge commits into the master branch, let’s now switch over to the master branch.

$ git checkout master
$ git merge ISSUE1

Now that ISSUE1 has been successfully merged with master, we can delete it.

$ git branch -d <branchname>

Personal access token (PAT)

You can also use personal access tokens to authenticate against Git over HTTP. They are the only accepted password when you have Two-Factor Authentication (2FA) enabled.

First, you need to create a personal access token (PAT). This is described here:

$ git clone
Username: <my-username>
Password: <my-personal-access-token>
$ git push
Username: <my-username>
Password: <my-personal-access-token>

Switch git repository to a particular commit

git checkout 6e559cb95

If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example:

git checkout -b <new-branch-name>
git checkout -b newbranch 6e559cb95

To get back to master commit

git checkout master

Rename a Tag on Git

git tag new old
git tag -d old
git push origin :refs/tags/old
git push --tags

Delete a Tag on Git

git push origin :refs/tags/tagname
git tag --delete tagname

See all Branches

git branch

Create a Branch from previous commit

git branch branchname <sha1-of-commit>

Push the new Branch to server

git push origin BRANCH_NAME

Clone a specific Git branch

git clone -b <branch> <remote_repo>

Rename the master branch name

git branch -m old_branch new_branch # Rename the branch locally
git branch -m new_branch # Rename the currently checked out branch
git push origin new_branch
# Then go to GitHub UI settings/options, if the old branch was 'master', change 'Default Branch' to another branch that is not 'master'. Then
git push origin :old_branch # Delete the old branch

Rename a local and remote branch

Rename your local branch

#If you are on the branch you want to rename
git branch -m new-name
#If you are on a different branch
git branch -m old-name new-name

Delete the old-name remote branch and push the new-name local branch

 git push origin :old-name new-name

Reset the upstream branch for the new-name local branch

 #Switch to the branch and then
 git push origin -u new-name

Delete a Git Branch Both Locally and Remotely

// delete branch locally
git branch -d localBranchName

// delete branch remotely
git push origin --delete remoteBranchName

Moving uncommitted changes to a new branch

git checkout -b newBranch
git status

Revert uncommitted changes including files and folders

# Revert changes to modified files.
git reset --hard

# Remove all untracked files and directories. (`-f` is `force`, `-d` is `remove directories`)
git clean -fd

Pull rebase

Rebase your local changes on the newest codebase.

git pull --rebase

Remove a Commit That Is Already Pushed to Git

Firstly, find out the comit that you want to revert back to.

git log

For example, commit 7f6d03 was before the 2 wrongful commits. Force push that commit as the new master:

git push origin +7f6d03:master

The + is interpreted as forced push. You can also use git reset to undo things. Then force push.

git reset 7f6d03 --hard
git push origin -f

Reset local repository to be like the remote repo

Setting your branch to exactly match the remote branch can be done in two steps:

git fetch origin
git reset --hard origin/master

If you want to save your current branch’s state before doing this (just in case), you can do:

git commit -a -m "Saving my work, just in case"
git branch my-saved-work

Retrieve the remote git address

git remote -v

Retrieve the active commit

git log --pretty=format:'%h' -n 1


To create shortcut for long commands. Below defines loagd instead of log --oneline --all --graph --decorate. Config file location can be --global, --system, --local, --file, --blob.

git config --global alias.loagd "log --oneline --all --graph --decorate"

To list the content of git config

git config --global --list

To find the place of a command

git config --show-origin alias.loagd

To see the content of a command

git config --get alias.loagd

Merge Strategies


Explicit Merge: Creates a new merge commit. (This is what you will get if you used --no-ff.)

Explicit Merge

Fast Forward Merge: Forward rapidly, without creating a new commit:

Fast Forward Merge

Rebase: Establish a new base level:


Squash on Merge: Crush or squeeze (something) with force so that it becomes flat:

Squash on Merge

Useful commands

Listing the N=10 largest files

git ls-tree -r -t -l --full-name HEAD | sort -n -k 4 | tail -n 10