Dec 14th, 2015

Sign off your commits

git commit --signoff --message 'This is my commit message'
git commit -s -m "This is my commit message"

Submitting a PR from a specific branch

To resync your main branch with all the changes.

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

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 main branch 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 main branch, let’s now switch over to the main branch.

$ git checkout main
$ git merge ISSUE1

Now that ISSUE1 has been successfully merged with main, 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 main branch commit

git checkout main

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

Delete a submodule

Remove the submodule entry from .git/config

git submodule deinit -f path/to/submodule

Remove the submodule directory from the superproject’s .git/modules directory

rm -rf .git/modules/path/to/submodule

Remove the entry in .gitmodules and remove the submodule directory located at path/to/submodule

git rm -f path/to/submodule

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>

Clone including submodules

git clone --recurse-submodules -j8 <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
// or shorter
git push origin :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 main:

git push origin +7f6d03:main

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 HEAD -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/main

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


git checkout -b development
(do your work on "development" branch)
git status
git commit -am  "updates in development branch"
git checkout main
git merge --no-ff development
git branch -d development
git push origin main

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

Common commands

  • git init: initializes a new
  • git add: stages files
  • git commit: commits staged files
  • git branch: shows current branches
  • git branch [branch]: creates new topic branch
  • git checkout [branch]: switched HEAD to specified branch
  • git merge [branch]: merges [branch] into current branch
  • git diff: shows diff of unstages changes
  • git diff –cached: shows diff of staged changes
  • git log: shows history of current branch
  • git log m/[codeline]: shows commits that are not pushed

Useful commands

Listing the N=10 largest files

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