0

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.

Ben Lesh
  • 105,049
  • 47
  • 242
  • 231

4 Answers4

2

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
Ben Lesh
  • 105,049
  • 47
  • 242
  • 231
1

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.

James World
  • 27,233
  • 7
  • 80
  • 112
0

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?

Community
  • 1
  • 1
GreenAsJade
  • 14,002
  • 9
  • 54
  • 91
  • So let's say that my 9 step tutorial is complete, but I need to make a fix at step 3 that will propagate through all of the other steps... – Ben Lesh Jan 19 '14 at 23:15
  • Ah - I didn't pick up the fact that you need to propagate the change: my current answer only helps to change a single step that is individually wrong. I'm not aware of a way to make a change "carry through" automatically. You can't change existing commits (AFAIK). So you need 6 more commits to tag with the current tags. In this instance, I think what you might do is branch at step 3, creating step 3a, which you retag with the tag on currently on step3. Then merge the current step4 into the new branch, making step 4a, retag, rinse and repeat. The new branch is the one you run with. – GreenAsJade Jan 19 '14 at 23:24
  • It seems like I should be able to insert a commit, then move the tag. Git is just pushing around deltas, after all... – Ben Lesh Jan 19 '14 at 23:27
  • You are right - that sounds reasonable in principle. I would have to bow out on trying to help with it in practice, though, I've never tried to dabble in that sort of magic! – GreenAsJade Jan 19 '14 at 23:29
  • I got curious. I found this: http://stackoverflow.com/questions/645450/git-how-to-insert-a-commit-as-the-first-shifting-all-the-others. It talks about attaching subsequent changes to an empty node, but I can't see why it won't work for a not-empty node. "rebase" sounds like it might be your friend. – GreenAsJade Jan 19 '14 at 23:34
0

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.

Niek Tax
  • 811
  • 9
  • 29