Ben Lesh
Ben Lesh

Reputation: 108491

Alter git history at a specific tag?

So I'm using Git for a something a little strange, basically I'm creating a tutorial and I'm tagging it at different stages of the process. At each tag the README is different. I realized there was a typo in the README at a previous step.

How do I insert a change at a specific tag and have it carry through?

  1. I realize this isn't what Git is for.
  2. I'm open to better ideas.
  3. Thanks in advance.

Clarification: I think I want to insert a commit, and then move the tag. Just not sure how.

Upvotes: 0

Views: 171

Answers (4)

Ben Lesh
Ben Lesh

Reputation: 108491

So here's what I ended up doing:

git checkout master

# create a copy of master
git branch master_copy

# move back to the tag where I want to change something
git reset --hard tagname

# (make changes here)
# and commit them
git commit -am "fixed something"

# tag it with a temp tag
git tag temp_tag

# delete the old tag
git tag -d tagname

# merge master_copy back onto it
git merge master_copy

# rename temp_tag to tagname
git tag tagname temp_tag
git tag -d temp_tag

# get rid of the master_copy 
git branch -d master_copy

Upvotes: 2

Niek Tax
Niek Tax

Reputation: 841

Git tag is indeed the way to go for your tutorial situation, I have also seen it being used in other git-based tutorials. Now we are talking about changing git history it may also be worthwhile mentioning http://rtyley.github.io/bfg-repo-cleaner/, a tool that was designed to handle cases in which wrong stuff ended up in commits that were already pushed. It may not completely fit your situation, but I can imagine that people who do benefit from this tool might end up finding this stackoverflow question.

Upvotes: 0

James World
James World

Reputation: 29786

Commits in git are inviolate - you can't change a commit. A commit is defined recursively - it includes its previous commit. So you can not just change a single point in history.

You can checkout the last good commit - before the mistake - then make a good commit. Now you will need to rebase all the changes from the tag following the mistake to the last tag you made.

Then you will need to move the tags created after the mistake on to the re-based commits (use git tag with the -f option).

In pseudo code (you would use the SHA's of the commits instead of the numbers here):

3
|
2
|
1 - mistake 
|
0

git checkout 0
* make changes *
git commit -m

3
|
2
|
1  1' (good)
| /
0

git rebase --onto 1' 1..3
* fix merge conflicts etc.*

3 3'
| |
2 2'
| |
1 1'
|/
0

Now you will have to delete the tags on 1,2,3 and recreate them on 1', 2' and 3' (or move them with the force option on git tag). With the old tags gone (assuming any branch names on the old commit are also deleted), a git gc will delete the old commits.

Upvotes: 1

GreenAsJade
GreenAsJade

Reputation: 14685

If I understand correctly, your tutorial users will check out tags in turn to move through the tutorial.

In this case, you need to make a new commit with the error fixed, then move the tag to that commit.

I think that you will need to branch from the current commit with the tag that we are talking about, commit the branch with the fixed README, then move the tag.

You can read about how to actually move the tag here: How can I move a tag on a git branch to a different commit?

Upvotes: 0

Related Questions