Skip to content

Instantly share code, notes, and snippets.

@dylanpyle
Created October 5, 2015 20:54
Show Gist options
  • Save dylanpyle/561f14c1007826e77faa to your computer and use it in GitHub Desktop.
Save dylanpyle/561f14c1007826e77faa to your computer and use it in GitHub Desktop.
### Initial situation: branch `develop` has one commit ("Initial Commit")
$ git checkout -b first develop
$ echo 'First content' > README.md
$ git add .
$ git commit -m 'First content'
[first 516f527] First content
### "first" branch now has two commits - "initial commit" and "first content"
$ git checkout -b second develop
$ echo 'Second content' > README.md
$ git add .
$ git commit -m 'Second content'
[second 6c2dfe5] Second content
### "second" branch now has two commits - "initial commit" and "second content"
$ git rebase first
First, rewinding head to replay your work on top of it...
Applying: Second content
Using index info to reconstruct a base tree...
M README.md
Falling back to patching base and 3-way merge...
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Failed to merge in the changes.
Patch failed at 0001 Second content
The copy of the patch that failed is found in:
/Users/dylan/dev/git-scratchpad/.git/rebase-apply/patch
When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".
$ cat README.md
<<<<<<< 516f527abc28ed9b373cfa05005e8e0aa119ebd4
First content
=======
Second content
>>>>>>> Second content
$ echo 'First content\nSecond content' > README.md
$ git add .
$ git status
rebase in progress; onto 516f527
You are currently rebasing branch 'second' on '516f527'.
(all conflicts fixed: run "git rebase --continue")
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: README.md
$ git rebase --continue
Applying: Second content
$ git log
commit 91e91062c322bbae535dfa0bd123699cc757479a
Author: Dylan Pyle <me@dylanpyle.com>
Date: Mon Oct 5 13:41:13 2015 -0700
Second content
commit 516f527abc28ed9b373cfa05005e8e0aa119ebd4
Author: Dylan Pyle <me@dylanpyle.com>
Date: Mon Oct 5 13:40:51 2015 -0700
First content
commit 422f5e30a9b061d8f7b169c508593df2270ec20d
Author: Dylan Pyle <me@dylanpyle.com>
Date: Mon Oct 5 13:40:19 2015 -0700
Initial commit
# End result: The "second" branch only ends up with one copy of each commit;
# We rewrote the history of the second commit, as it changed from a commit
# based on the content of `develop` to a commit based on the content of
# `develop` and `first`.
#
# Compare the commit hashes at the time we committed them, to the final result:
#
# Before:
# first = 516f527
# second = 6c2dfe5
#
# After:
# first = 516f527
# second = 91e9106
#
# The commit called "Second content" is not the same commit as the one we
# initially wrote. GitHub will show them as different from each other, if you're
# comparing two branches. To resolve this, you'll want to force push the second
# "re-written" commit over the first one in cases where it's appropriate -
# otherwise you'll end up with two of the same (or similar) commits in your tree
@rargulati
Copy link

This is great - thanks!

Only question: under what circumstances will this operation require a force push? As it's mostly a destructive operation, notes on etiquette and when a force push is acceptable would be great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment