You have a branch based on origin/develop
(commit 300
). It has 2 commits
My commit 1
and My commit 2
. origin/develop
has a new commit commit 301
. Now you need to rebase your branch so your commits are on top of the
current origin/develop
.
Tips:
- You need to know which commits are in your branch:
My commit 1
andMy commit 2
. - Use
git rebase -i origin/develop
(interactive) and pick only the commits in your branch. - Every one of the commit diff should look almost the same as ones before you rebase (assuming there is no reimplementation).
- Since the commit hashes changed (the history of your branch), you will need to force push to
origin
. - Instead of maintaining a local
develop
branch, just useorigin/develop
unless you are working ondevelop
. - Rebase replays your commits on top of
origin/develop
, which means you have to resolve conflicts for each of your commits. It is therefore helpful to squash wip commits beforehand.
Steps:
-
Fetch updates from
origin
:git fetch -p
git log --graph --oneline --decorate origin/develop HEAD
* a6b4235 (HEAD -> my-branch) My commit 2 * 99b0f26 My commit 1 | * b44e301 (origin/develop) commit 301 |/ * a407a19 commit 300 * eefda89 commit 299 * cbea031 commit 298
-
Use interactive rebase to pick commits: (Remove any lines that contain commits that are not yours)
git rebase -i origin/develop
This opens an editor with something like this:
pick a407a19 commit 300 pick 99b0f26 My commit 1 pick a6b4235 My commit 2
Make sure to remove the line
pick a407a19 commit 300
, it is not one of your commits so should not be in your branch:pick 99b0f26 My commit 1 pick a6b4235 My commit 2
There are other commands like "edit", "fixup", "squash" etc. Try them out too!
Save the file and quit
-
Resolve any conflicts and continue the rebase:
git add file-with-conflicts.txt
git rebase --continue
-
Check that your commits still make sense:
git log --graph --oneline --decorate origin/develop HEAD
It should look something like:
* 0f0b7da (HEAD -> my-branch) My commit 2 * 74ff1b8 My commit 1 * b44e301 (origin/develop) commit 301 * 9727723 commit 300 * eefda89 commit 299 * cbea031 commit 298
To check the changes in each commit:
git show --reverse origin/develop..
To check all changes compared to the branch on origin:
git diff origin/my-branch..
-
Force push to
origin
:git push -f origin HEAD