Search code examples
gittagsbranchgit-remoterevert

Move branch one step back to leave a branchless tagged commit


Here is my current situation, working on branch draft:

* 51c716b (HEAD -> draft, tag: Printed, remote/draft) Dirty squeezing of the draft to be printed on as few pages as possible
* 9219af9 Draft ready for reading
* e3366b2 Making progress on the draft
* 163ab8f (master, remote/master) Create a draft

Now that I have got feedback about this Printed version of the draft, I wish to go on with the project. Obviously, the latest commit 51c716b (tagged as Printed) is not the one I want to resume from: I want to resume from 9219af9. But I don't want to loose 51c716b.

What should I do for the situation to be like this after my next commit?

* b57cc00 (HEAD -> draft, remote/draft) Integrate feedback from readers.
| * 51c716b (tag: Printed) Dirty squeezing of the draft to be printed on as few pages as possible
|/
* 9219af9 Draft ready for reading
* e3366b2 Making progress on the draft
* 163ab8f (master, remote/master) Create a draft

(I also wish that the tag Printed were available on remote.)

Note: I am aware that this would cause trouble to people having pulled on draft while it was pointing to 51c716b. But I am confident that nobody has done such a thing ;)


Solution

  • You're probably looking for Reset. Git branches are references pointing to commits, so you basically want to move the local reference draft to point to 9219af9. You can do so with:

    git reset --hard 9219af9

    Or, if you were to use ancestry references:

    git reset --hard HEAD~1

    After that, you will probably create a new commit (b57cc00 in your example), and you'll have to update the remote reference draft (the one that is in origin) to point to the new commit. You can do that with:

    git push origin draft --force-with-lease

    This will tell the remote that you are aware that you are "loosing" 51c716b. However, Git will still keep commits that have references pointing to it and, since you have a tag (which is also a reference) on 51c716b, you'll always be able to go back to it. Make sure you push your tag if you want other people (or your future self) to be able to access it:

    git push origin Printed