Skip to content

Git tips

Frederic Bastian edited this page Oct 9, 2015 · 9 revisions

This page aims at providing useful references about Git.

Branching

git checkout -b <branchName>
... edit files, add and commit ...
git push -u <remote> <branchName>

See http://stackoverflow.com/a/6232535/1768736

  • To delete a branch locally and remotely
git branch -d <branchName>              #delete local branch
git push <remote> --delete <branchName> #delete from remote (e.g., 'origin')

Afterwards, it is recommended to make a pruning on all computers using the project, to remove obsolete tracking branches

git fetch <remote> --prune # Delete multiple obsolete tracking branches
git fetch <remote> -p      # Shorter

# Note, to see you remote tracking branches:
git branch -r              

See http://stackoverflow.com/a/23961231/1768736

  • To merge branches, it is recommended to forbid fast-forward merges even if possible, to always get a specific commit for the whole merge:
git merge --no-ff <targetBranch>

Advanced merge and rebase

Put back last commit into staging area

git reset --soft HEAD^

Amending any commit (not only the last one)

To amend your last commit, you can use git commit --amend, but what about modifying a previous commit? (from http://stackoverflow.com/a/1186549/1768736). Warning: never amend a commit that has been made public on a remote repository.

  • use git rebase -i SHA1^.
  • in the default editor, modify 'pick' to 'edit'
  • make the changes you want, add them, etc, then, amend your commit and continue the rebasing:
git rebase -i SHA1^  #SHA1: hash of the commit to amend
#in the editor, modify 'pick' to 'edit' for the targeted hash
git add ...          #modify as you wish, or use 'git reset HEAD^' to split the commit
git commit --amend   #add --no-edit if you don't want to edit the commit message
git rebase --continue

Cherry-picking a specific commit from a branch

  • To merge into a branch one specific commit form another branch, use the cherry-pick command:
git checkout <branchName>
git cherry-pick SHA1       #hash of the targeted commit
  • You can avoid merging directly the commit into your branch, to review the changes first, by using the option --no-commit:
git checkout <branchName>
git cherry-pick SHA1 --no-commit      #hash of the targeted commit
git add -A                            #add the modifications as you wish

Cherry-picking several commits from a branch (and modifying them if needed)

If you'd like to cherry-pick only some specific commits from a branch, but do not want to do it manually one commit at a time, or if you'd like to modify these commits before including them:

  • You need to do a rebasing, from the branch where are the commits to cherry-pick (let's call it branch-with-commits), onto the branch where you want to add the commits (let's call it master):
git checkout branch-with-commits
git rebase -i master

You will then see all the commits from branch-with-commits that will be rebased on top of the master branch commits. You can remove any commit, modify them, etc.

  • Warning: never rebase commits that have been push to the remote repository. In that case, create a new branch to do the rebasing:
git checkout branch-with-commits
git checkout -b cherry-pick-branch
git rebase -i master

Preparing a highly-diverged branch before merge

(see https://www.atlassian.com/git/tutorials/merging-vs-rebasing/conceptual-overview)

In some cases, a feature branch and the main branch have diverged so much that the merge will be messy. As an alternative to merging, you can rebase the feature branch onto the main branch before merging, e.g.:

git checkout feature-branch
git rebase -i master
#[Clean feature-branch]
git checkout master
git merge feature-branch
  • During the interactive rebasing, you can edit a commit before including it (modify 'pick' to 'edit', see "amending a commit" above)

  • You can even edit the modifications inside a file before including them using git add --patch or git add -p, see http://stackoverflow.com/a/1085191/1768736 and http://nuclearsquid.com/writings/git-add/

  • Warning: never rebase commits that have been made public on a remote repository. In that case, create a temporary branch to do the rebasing, e.g.:

git checkout feature-branch
git checkout -b clean-feature-branch
git rebase -i master
# [Clean up the history]
git checkout master
git merge clean-feature-branch

From https://www.atlassian.com/git/tutorials/merging-vs-rebasing/conceptual-overview: "This moves the entire feature branch to begin on the tip of the master branch, effectively incorporating all of the new commits in master. But, instead of using a merge commit, rebasing re-writes the project history by creating brand new commits for each commit in the original branch.

The major benefit of rebasing is that you get a much cleaner project history. First, it eliminates the unnecessary merge commits required by git merge. Second, as you can see in the above diagram, rebasing also results in a perfectly linear project history—you can follow the tip of feature all the way to the beginning of the project without any forks. This makes it easier to navigate your project with commands like git log, git bisect, and gitk."