Git Notes

Merge develop branch into master

git checkout develop
git merge master

At this point, if there are any merge conflicts, you can resolve them.

git checkout master
git merge develop
git push origin master


Cherry Picking

git-cherry-pick is used when you want to take a commit from one branch and apply it to a commit in another branch. This can be thought of as a more surgical version of git merge (applying one commit vs applying multiple commits).

git cherry-pick <hash of commit>

Syncing a fork

When you're introducing changes to another open source project, sometimes your fork gets out of sync with the original project. To fix this, do the following:

git fetch upstream
git checkout master
git merge upstream/master

In the event that you get this error:

fatal: 'upstream' does not appear to be a git repository

Check if the upstream for the original project is set:

git remote -v

If it isn't, get the url for the git repo and run it with this command:

git remote add upstream<original project>

For example, if I was working on my copy of the xssValidator project, and wanted to add the upstream for the original:

git remote add upstream

Remove latest commit from GitHub

Pushed too soon or by accident? Let's fix that.

  1. git rebase -i HEAD~2
  2. Delete the commit (should be the second line in the text file)
  3. git push origin +<branchName> - for example: git push origin +master

Redo git commits that have been pushed to remote

  1. Check out the branch you want your commits to be in: git checkout <branch>
  2. Find git ref id one commit before the commit you want to change
  3. git rebase -i <ref #>
  4. Remove commits you do not want in this new branch
  5. Edit the messages for commits as needed.
  6. Save the file.
  7. git commit --amend - change the commit message to what you want
  8. git rebase --continue
  9. Once done with commits, git push -u origin <name of branch>

Merge file from one branch to another

git checkout <branch to merge from> <file name>

Update branch with another

  1. git checkout <branch to overwrite>
  2. git rebase origin/master - to rebase all changes on this branch on the tip of master

Amend commit

  1. git add <whatever you forgot to include>
  2. git commit --amend

Stash queued changes to go to another branch && restore them

  1. git stash
  2. Do whatever in other branch
  3. Go to branch that was stashed
  4. git pop

Fix github sync issue:

git stash save --keep-index
git stash drop

Delete file from github history (in case sensitive information goes up on accident):

git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch <filename>'
git push origin --force --all

Change authors of pushed commits with rebasing

  1. Find the commit before (chronologically) the first commit that you want to change.
  2. git rebase -i <SHA hash of the commit before the one you want to change>
  3. Change the lines for the commits you want to modify by changing them from pick to edit.
  4. Save the file once you're done.
  5. Change the commit to the author you want to be reflected like so: git commit --amend --author="New Author Name <>".
    For example:
    git commit --amend --author="Jayson Grace <>"
  6. Once you've changed the author for the commit, continue on through the rebase using git rebase --continue until you've resolved all commits.
  7. You may need to deal with some errors that come up, so be sure to fix those, and then git add the file that you've had to resolve before using git rebase --continue.
  8. Finish by using git push --force


Change author of last commit

git commit --amend --author="Philip J Fry <>"


Undo last commit and keep changes made

git reset HEAD~1



Add the existing git repo as a submodule of the current repo:

git submodule add <git repo> <destination path - optional>

This should generate a .gitmodules file and the folder of the repo you intended to submodule.

Update submodules recursively

git submodule update --init --recursive

Clone a repo with submodules in it:

git clone --recurse-submodules <repo with submodules>

Update upstream changes to submodules:

git fetch
git merge origin/master


Update submodule to latest version

cd submodule_name
git checkout master && git pull
cd ..
git add submodule_name
git commit -m "updating submodule to latest"


Remove a submodule

git submodule deinit <path_to_submodule>
git rm <path_to_submodule>
git commit-m "Removed submodule "
rm -rf .git/modules/<path_to_submodule>


Add changes to a previous commit

If you don't want to change the commit message:

git commit --amend --no-edit

If you want to change the commit message:

git commit --amend -m "new commit message goes here"

You can also omit the -m and the message following it if you want to edit the commit in a text editor.

If you've already pushed your commit previously:

git push -f origin <branch>


Anonymous clone of git repo

git clone git://


Create deploy token in gitlab

This can be used if you need to automatically clone into a private gitlab repo as part of a CI/CD pipeline. Be sure to set an expiration date if you can get away with it for security.

  1. Login
  2. Go to repo
  3. Settings
  4. Repository
  5. Expand button next to Deploy Tokens
  6. Set scopes based on your criteria
  7. Click Create deploy token

Use it to clone the repo:
git clone https://<gitlab+deploy-token-number>:<token_password>



Change release commit

If you create a release and have to make some changes and then re-release, here's what you need to do:

git tag -f -a <tagname> # i.e. v1.0.0
git push
git push -f --tags


Collect information needed to add ssh key

Generate an ssh key using the information under CREATE SSH KEY found on this page

echo "Your public key:"
cat ~/.ssh/
echo "Your ssh key fingerprint is:"
ssh-keygen -lf ~/.ssh/id_rsa | awk '{ print $2 }'

Show staged changes

Useful if you have already run a git add and are trying to remember the changes you've made.

git diff --staged


Add existing key to new system

Add the private key to the new system you've created in ~/.ssh/some-key-you-have. Once this is done, run the following commands for a linux box:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/some-key-you-have

for OSX:

eval "$(ssh-agent -s)"
ssh-add -K ~/.ssh/some-key-you-have


Find shield for project

Squashing commits

Create a feature branch. Next, create two files:

echo "1" > file1.txt; git add file1.txt; git commit -m 1st file1.txt

echo "2" > file2.txt; git add file2.txt; git commit -m 2nd file2.txt

Push the changes to the remote repository:

git push --set-upstream origin <target repo>

Squash the two commits into one:

git push origin --delete <target repo>
git rebase -i HEAD~2

Change one of the entries to squash

Push the squashed commits:

git push -u origin <target repo>

Confirm there's only one commit in the git logs.