Save Git state prior to rebase

Viewed 253

When I'm going to do a Git operation that could be problematic (e.g. a merge/rebase that will require a lot of conflict resolution) my typical workflow is:

# was on `feature-branch` which has lots of conflicts with `master`
git checkout -b temp
git checkout feature-branch
git rebase master

This way, if I screw something up in the rebase I've saved the original state in the temp branch. I'm wondering if there is a better way to do this than create a temporary branch.

4 Answers

It is a good way to keep track of a prior state, I do this too (a branch, or a tag).

But you don't necessarily need to do that, since when you use git reflog <branch> (typically HEAD) you will see the history of <branch>, so you can reset to a commit the branch previously pointed to even if you don't first make a temporary branch for it.

Thing to know: a branch is just a name pointing to a commit.

Another thing to know: A rebase just copies commits, but then it moves the original branch pointer.

So what you're doing is sensible, although there is no need to checkout the temp branch and switch back. If you make another branch pointer, you have an easy way to repoint the original branch pointer again (with a hard reset) in case you don't like the way things turned out.

So before rebase:

A -- B -- C <-- main
      \
       X -- Y -- Z <-- branch

Prep step:

A -- B -- C <-- main
      \
       X -- Y -- Z <-- branch, temp

After rebase:

        main
          |
A -- B -- C -- X' -- Y' -- Z' <-- branch
      \
       X -- Y -- Z <-- temp

So just reset branch hard back to temp if you dislike the outcome, and let the copies it pointed to go out of existence. Then delete temp.

If you do like the outcome, delete temp, and let the original commits it pointed to go out of existence.

You need to perform git stash operations like so:

git stash create [<message>]

git stash branch <branchname> [<stash>]

Then keep this all aside and do whatever you want with the branch you are already working on.

Then if something not working with your current operation, you can use the previously saved branch by using:

git stash list

Or

git stash show

And finally, if you want to apply or remove that stash, simply use:

git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]

That's it

Link to the docs:

https://git-scm.com/docs/git-stash

I assume you push your feature-branch to your remote anyway, so you don't have to backup anything, since it is backed up on your remote. Just make sure you didn't break anything during rebase before force pushing the feature-branch.

So the workflow would be as follows.

Before rebase:

A -- B -- C <-- main, origin/main
      \
       X -- Y -- Z <-- branch, origin/branch

After rebase:

        main, origin/main
          |
A -- B -- C -- X' -- Y' -- Z' <-- branch
      \
       X -- Y -- Z <-- origin/branch

If everything worked well, you just run git push -f to update the remote feature branch.

        main, origin/main
          |
A -- B -- C -- X' -- Y' -- Z' <-- branch, origin/branch

And if it doesn't you hard reset to origin/branch and you are back at the beginning (git reset --hard origin/branch).

A -- B -- C <-- main, origin/main
      \
       X -- Y -- Z <-- branch, origin/branch

This is basically, the greatness of git and maintaining a remote repository, it's not just about collaboration with others, you can also collaborate with your past self :D

Related