3

Suppose I've been working on a branch called my-branch, occasionally merging commits from master to stay up-to-date. On some merges I had to resolve conflicts, which was time consuming.

Now I'm done with my-branch and I want to rebase it to one commit on top of master. During the rebase I'll have to resolve the same conflicts which is going to be tedious.

But I already have the exact state I need the files to be in, I just want to clean up the history. So the fastest way to rebase would be to:

  1. Rewind to master
  2. Make the files the same as in my-branch, but discarding the commit history. At this point I'd have a lot of uncommited changes.
  3. Make one commit

Is that possible?

EDIT: SO is telling me to explain why my question isn't a duplicate of this one. While the most upvoted answer there would solve my problem, the question there is "how to squash commits?" and mine is "I know how to squash commits, how to do it without dealing with conflicts?". So the answer is the same, but I didn't find it when looking for the solution before posting here. Perhaps if someone else also knows about rebase but doesn't know about soft resets, they'll find the solution here.

Rafał G.
  • 1,529
  • 22
  • 35
  • Possible duplicate of [Squash my last X commits together using Git](https://stackoverflow.com/questions/5189560/squash-my-last-x-commits-together-using-git) – Minn Feb 21 '19 at 17:59
  • Just went there to see the responses and I'm glad to see that the response using `git reset --soft` is way more popular that the one based on `git rebase`. – eftshift0 Feb 21 '19 at 19:29

1 Answers1

6

Sure.... it's fairly simple using git reset --soft.

git checkout my-branch
git merge master -m "Merging last changes from master"
# we start the magic here
git reset --soft master # set the branch pointer on master
git commit -m "Here's the work for X feature in a single shot"
eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • 2
    This is it, thank you. That's super simple, I guess it must be in the docs but I didn't quite understand it on the first read. Gotta revisit the basics every now and then. – Rafał G. Feb 21 '19 at 18:03
  • Heh. I used to tell people I read manuals for a living when I didn't want to talk about computers. – jthill Feb 21 '19 at 18:38