Forrest Keppler
Forrest Keppler

Reputation: 727

How do I make a git commit equal to the diff between my working tree and a branch?

Git can be a complicated and funny beast, and I have a habit of copy/pasting stack overflow answers without understanding how they work. The result is that I often find myself in a state where my working tree is fine, but my branch is so messed up that I dont want to figure out how to merge it back to main. It would look something like this:

A---B---C---D---Main
  \   *******************
    \ * Big pile o' git *---Good
      *******************

What I want to do here is take the good state I have and put it on top of main so I have a nice clean working tree:

A--B--C--D--Main+Good

Is there a way to take the diff between what I have on one branch and the head of another branch and make it a new commit on top of the first match.

Upvotes: 1

Views: 41

Answers (1)

LeGEC
LeGEC

Reputation: 51850

Here is one way to squash the changes on your branch to one single commit, and try to play it back on top of Main :

# starting from branch Good :
git checkout Good

# (optional) use a temporary branch if you want :
git checkout -b wip

# two commands to squash everyting into one commit on top of A :
git reset --soft A
git commit

# you can now use 'git rebase' :
git rebase Main

I guess you want the above solution, however, since your question is specifically worded as "I want to take the state of Good and place use that as the new state for Main", here is one way to do exactly that :

# starting from branch Good :
git checkout Good

# use reset --soft :
git reset --soft Main
git commit

note that this will completely replace the content of Main with the content of Good, and somehow ignore the modifications brought in by commits B--C--D--Main

Upvotes: 1

Related Questions