6.5. Git Tips and Tricks

6.5.1. General Description

The goal of this Wiki page is to collect and share hints & tricks about Git usage so that with all the working team.

Most of these proposals have been taken from internet, so you can find them by yourself, but all of the following examples have been tested already.

6.5.2. Git basics Fetch vs. Checkout vs. Pull vs. Cherry pick

There are various ways to update a local repository, depending on your use-case. It's good to know the differences between these available options. A link to the official documentation is provided also.

  • Fetch: Fetch branches and/or tags (collectively, "refs") from one or more other repositories, along with the objects necessary to complete their histories. Remote-tracking branches are updated.
  • Checkout: Fetches the latest changes. You should already have this repo downloaded. It does not merge those new changes but makes your working directory reflect them. You can merge them at your leisure later.
  • Pull: Fetches the changes AND merges them into the local branch of the same name.
  • Cherry-pick: Fetches the commit and plays it on top of the current local branch, thus creating an entirely new commit which happens to have same changes as the one it fetched. Get the list of Tags locally

Tags are not fetched locally by default when cloning or pulling a branch: you need to "fetch" them specifically, using the following command:

git fetch
git fetch --tags Show log

If you want to have a nice log, with a limited history, you can simply type the following command. It will provide you only the necessary information, and only the last 10 commits. You can of course change this number as you want.

git log --pretty=oneline -n 10 Delete Branch

In order to delete a local branch:

git branch -D 'branchName'

In order to delete a remote branch:

git push -d origin 'branchName' Delete Tag

In order to delete a local branch:

git tag -d 'tagName'

In order to delete a remote branch:

git push --delete origin 'tagName' Rename a git tag in the remote repo

If you want to rename a tag, you simply need to create a new one and delete the old one, locally and remotely:

git tag 'new' 'old'
git push --tags
git tag -d 'old'        // Delete a tag
git push origin :refs/tags/'old'
git ls-remote --tags origin Get all the commits where a file is mentionned

If you want to retrieve all the commits where a file is modified from all the branches of the repository:

gitk --all path_from_git_project_root/your_file

6.5.3. SSH Keys How to avoid typing the SSH Passphrase all the time ? Using TortoiseGit

When using TortoiseGit, you need to install "Puttygen" (to select during install) to generate your SSH Keys. It will also ask you a passphrase which you will nee to type only once during your working session (until you reboot your laptop). Putting your laptop at sleep doesn't affect this behavior. Using Git Bash or Cygwin

If you use Git in command line, either with Git Bash or Cygwin, you can also avoid typing your passphrase every time you type a command: you need to edit the file ".bashrc" located under "/home/'user'/" folder, and to add the following content:

eval $(ssh-agent -s)
ssh-add ~/.ssh/id_rsa

6.5.4. Advanced tips & tricks Merge master into branch

Before merging your branch into master, you first need to merge the master into your branch on order to simplify the automatic merge afterwards using GitLab.

Here are the steps to follow, using a command line tool (git bash or Cygwin), on your local repository :

git checkout master
git pull
git checkout (-b if necessary) 'working-branch'
git pull origin 'branch-name'
git merge master // git rebase master
## Resolve conflicts manually if required
git commit -am "message" Merging 2 different git repositories

git remote add 'remote_name' 'remote_project_path'
git fetch 'remote_name'
git status
git merge -S --allow-unrelated-histories 'remote_name'/master
/* Have fun resolving the conflicts! */
git add .
git commit -am "Your message"
git push origin 'branch' Rewrite commit messages after push

This action shall be done very carefully, it has been tested without much success, but it was about very old commits which implied lots of changes.

git rebase -i 'commit hash you want to change'
/* pick "reword" */
/* (editor will close and open again with the messages to edit) */
/* Edit the message */
/* Save & quit */
git push origin --force Revert master to previous commit

In case you did changes and commits (local and remote) that you want to revert, here is the preocedure to follow. The example provided is about the master branch but it's applicable to any branch.

git checkout master
git reset --hard 'commit you want to apply'
git push --force origin master
/* Then to prove it (it won't print any diff) */
git diff master..origin/master Import file changes from a branch to another branch

In case you want to import changes that were done in a file in another branch without merging the source branch in the target branch, here is the procedure to follow.

/* Switch to the target branch */
git checkout 'target_branch'
/* Import the file */
git checkout 'source_branch' -- 'file to import'