-
Notifications
You must be signed in to change notification settings - Fork 12
Git Cookbook
Load the branch to update:
git checkout mybranch
Make sure pending changes are committed:
git status
git add ...
git commit -m "message describing the change"
or stash (hide) them temporarly:
git stash
To reapply stashed changes:
git stash pop
Get up-to-date branches from the remote repository (origin
) in the local repository (saved as origin/<branch name>
- for instance origin/master
is the local copy of branch master
from the origin
repository):
git fetch
To fetch branches from another remote:
git fetch <remote repository name>
Rebase: move commits of the current branch on top of a the local (refreshed) copy of the branch master (ie. origin/master
). New commits are placed at the end.
git rebase origin/master
Figure out what files are conflicting:
git status
Edit files to fix conflicts and mark conflicts as solved by adding the edited files, then continue rebasing:
git add <edited files>
git rebase --continue
If it's really a mess: cancel:
git rebase --abort
All those instructions are documented when a problem is detected while rebasing.
It is possible to interactively reorder commits in the current branch as well as to remove them or merge some commits together.
First thing is to get the hash ("id") of the commit right before the list of commits to edit:
git log
Display the list of commits made after some commit and manipulate it in a text editor (-i
= interactive):
git rebase -i <hash of the preceding commit>
In the text editor window it is then possible to reorder commits (by changing the commit lines order), removing some of them (remove the lines), or merge some together ("squash"). It is also possible to change commit messages. All actions are described in comments below the list of commits in the text editor window.
fixup
is quite handy: it merges the commit with the previous one, keeping only the previous commit message.
Be aware that reordering/removing/squashing commits may create conflicts.
When done with commits manipulation, save and close editor => rebase is then triggered. If no change was done in the commits list, the rebase does nothing.
When changes must be added to the very last commit, instead of rebasing it as above, it is possible to simply amend it:
git add <files to add the previous commit>
git commit --amend
Rebasing/amending causes the commits tree to be rewritten, thus being incompatible with the branch available on the remote repository. Doing a usual git push
will then be reported as failing.
It is required to force the push:
git push origin mybranch -f
Be aware that a forced push will replace the former version of the branch on the remote repository. It is irreversible except if someone else still has a copy of the old branch.