-
Notifications
You must be signed in to change notification settings - Fork 1
Git tips
This page aims at providing useful references about Git.
-
Excellent tutorial about use of branching in Git: https://www.atlassian.com/git/tutorials/using-branches
-
To push a new local branch to remote, and to make it immediately trackable, so that
git pull
andgit push
work immediately:
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>
git reset --soft HEAD^
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
-
You can even edit the modifications inside a file before including them using
git add --patch
orgit add -p
, see http://stackoverflow.com/a/1085191/1768736 and http://nuclearsquid.com/writings/git-add/ -
If you wanted to split the commit, once you moved to the desired commit using rebase, you can do
git reset HEAD^
, then, pick and commit the files as you wish.
- 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
- You can even edit the modifications inside a file before including them using
git add --patch
orgit add -p
, see http://stackoverflow.com/a/1405189/1768736, http://stackoverflow.com/a/1085191/1768736 and http://nuclearsquid.com/writings/git-add/
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 itmaster
):
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
- During the rebasing, you can also 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
orgit add -p
, see http://stackoverflow.com/a/1085191/1768736 and http://nuclearsquid.com/writings/git-add/
(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
orgit 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
."