Git cheat sheet

Usual actions

Save and recover local changes

  • git stash save: save local changes temporarily
  • git stash pop: recover local changes

Revert the latest commit

git revert HEAD

Revert non pushed commits on development

git reset --hard origin/development

Revert non pushed commits on a branch

git reset --hard <BRANCH-NAME>

Revert a merged branch

1) Change to the development branch
2) Clean your local changes

git reset --hard

3) Get the latest version of the development branch

git fetch
git pull

4) Look for the ids of the commits to revert

git log

5) Revert one by one the committed merges, reverting first the latest merged one

git revert xxx

6) Check that the commits appear as reverted in the history

git log

7) Push the changes to the branch

git push

Fix the message of a pushed commit

For example if you have forgotten to add the ticket number to the message:
1) git commit –amend -m “New commit message”
2) git push –force origin <BRANCH-NAME>
Note: only do it in your branch because doing it on development or master may cause a lot of mess.

Rename a remote branch

For example to remove a character that causes problems:
1) Checkout the branch which you want to rename
2) Rename and push it:

git branch -m old_branch new_branch
git push origin new_branch

3) Remove the old branch from remote:

git push origin :old_branch_name

Abort a merge that has conflicts

git merge --abort

Abort a rebase that has conflicts

git rebase --abort

Check if a branch has been merged

git branch --no-merged | grep 'branch_name_or_number'

Change to someone’s branch to work together on it

1) Get the references of the latest branches on Git:

git fetch

2) Change to the branch:

git checkout <BRANCH-NAME>

After that you should be able to see what the other person has committed and pushed to it and and you will be able to work on it as if you had created the branch.


When we are going to merge changes into a branch and Git rejects it due to merge conflicts:
1) Change to the development branch and get the latest version of the changes:

git checkout development
git fetch
git pull

2) Change again to your branch:

git checkout <BRANCH-NAME>

3) Rebase to add to the branch the latest changes from development:

git rebase development

Git will say that there are conflicts to solve. In Intellij right click on the project, click on Git, and it will appear an option to solve conflicts. After clicking on it will appear a window that helps showing the two versions of the file side by side.
4) Once you finish solving it run:

git rebase --continue

5) Once you finish rebasing, commit your changes to your local branch:

git commit

You may be asked to save the commit message for the rebase. In most cases you will just want the default message, so you can save it using the command “:wq” of vi:
6) And do the last steps to push your changes to the remote branch:

git pull
git push

You should be able now to merge the branch.

Key management

Configure SSH keys

In order to be able to connect from a new host:
1. Generate a SSH key that will be used to authenticate to Gitlab:
1.1. Go to the folder containing the SSH keys: cd ~/.ssh
1.2. Run this command to generate a public/private key pair and enter a meaningful file name:

ssh-keygen -t rsa -b 4096 -C "REPLACE_EMAIL"

2. Configure the SSH key in the Git provider:
2.1. Get the content of the public key file running e.g.:


2.2. Go to the Git provider, navigate to your settings and there to the section about SSH Keys
2.3. Add the copied value of the public key there and enter something meaningful to specify where that key was generated
3. Configure the SSH key in the host:
3.1. Add something like this to ~/.bashrc or ~/.bash_profile to load the key automatically:

# Load the SSH key to connect to Git: 
eval $(ssh-agent -s)

3.2. Add something like this to: ~/.ssh/config

  PreferredAuthentications publickey

3.3. Change the permissions of this config file to 600. If not you will get this error: “Bad owner or permissions on … fatal: Could not read from remote repository.”

chmod 600 ~/.ssh/config

4. Check the key works running on the box:
4.1. Reload configs by running “exit” and then connecting again via SSH
4.2. Check you can connect from the terminal:


5. Configure the username and email for the commits. If not you will have to revert or edit your commits as “git push” will fail saying that you don’t have permissions on the repository.

git config --global 'REPLACE_NAME_AND_SURNAME'
git config --global 'REPLACE_EMAIL'

Load Git SSH keys

eval $(ssh-agent -s)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>