Skip to content

Instantly share code, notes, and snippets.

@lukeredpath
Created January 11, 2013 12:24
Show Gist options
  • Save lukeredpath/4510308 to your computer and use it in GitHub Desktop.
Save lukeredpath/4510308 to your computer and use it in GitHub Desktop.
State A (current):
branch foo: A -- B -- C -- D
\
branch bar: -- E -- F -- G --H
I want commit E from bar brought back into foo, without cherry-picking and creating a new commit. Essentially I want to go to this:
State B:
branch foo: A -- B -- C -- D -- E
\
branch bar: F -- G --H
@caius
Copy link

caius commented Jan 11, 2013

$ git checkout foo
$ git merge <sha of commit E>

Seems to do what you want from what I just did locally.


mkdir -p ~/tmp/lukeredpath
cd !$

git init
# Rename master to foo
git branch -M foo

# Add commits A-D on `foo`
git commit --allow-empty -m "A"
git commit --allow-empty -m "B"
git commit --allow-empty -m "C"
git commit --allow-empty -m "D"

# Branch `bar` from `foo`
git checkout -b bar

# Add E-H on `bar`
git commit --allow-empty -m "E"
git commit --allow-empty -m "F"
git commit --allow-empty -m "G"
git commit --allow-empty -m "H"

# Go back to `foo`
git checkout foo

# Merge in commit "E" (SHA is the SHA of commit "E")
git merge 0b1ba4357d706cab759077b29c57570c9a6b2237

# Check log of `foo` contains E
git log

@olly
Copy link

olly commented Jan 11, 2013

I think you could just do (from branch foo):

git reset --hard E

(where E is the sha reference to E)

This would point that branch at commit E. The history stays intact. This only won't work if you have further commits (not shown) on branch foo.

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