How can I edit a previous commit and without erasing the commits ahead of it in git? [closed]

StackOverflow https://stackoverflow.com/questions/22254300

  •  11-06-2023
  •  | 
  •  

Question

I attempted to branch from a previous commit (not HEAD), but when I committed those changes, rather than keeping the change in the branch it obliterated my commits past that point in HEAD. I can see how this could be useful, but I thought the point of a VC was to keep all commits forever so that you could always go back to a previous state if you wanted to.

How do I edit a previous commit and submit it without erasing the commits ahead of it on the master?

Thanks!

EDIT:

This is the image that lead me to believe my other changes had been deleted

This is the image that lead me to believe my other changes had been deleted

Here are the hidden branches I thought had been deleted

Here are the hidden branches I thought had been deleted. Why does gitk not show the other branch(HEAD,master)

Was it helpful?

Solution 2

The commits probably aren't lost. Make a backup first. Check what branches you have, and what's in them. You might be interested to learn about the reflog, if you really can't find your commits in any branch.

If you don't have a repository browser installed (I like tig), you could inspect your repository using a command like this: git log --graph --all --oneline --decorate

How do I edit a previous commit and submit it without erasing the commits ahead of it on the master?

If the commit to edit is the previous commit:

  • Make the changes you want.
  • Stage the changes, probably using git add -u if you're not adding new files.
  • Amend the previous commit, git commit --amend will do that, add -C HEAD if you don't want to edit the commit message.

If you want to edit an older commit, you need to use rebase. This can be done interactively:

  • Start from whichever branch you want to edit, usually with no changes in your working copy.
  • Make the changes you wanted in the earlier commit.
  • Stage and commit those changes - don't bother writing a sensible commit message here.
  • Run git rebase -i, this will open an editor:
    • Move the last line (with the nonsense commit message) up until it is on the line below the commit you want to edit.
    • Change pick at the start of the line to f (for fixup).
    • Make no other changes to the file
    • Save and exit
  • Git should modify your earlier commit to include the edit, and will try to rebase your other changes on top.

This will try to apply your edits to the earlier commit, and rebase all your subsequent changes on top of the earlier commit plus your edits. You might get merge conflicts if the later commits are affected by your edit. Git will provide instructions on how to resolve these. If you don't understand them, you can use git rebase --abort to go back to where you started, with your edits being the latest commit.

Note that you should not edit commits that you have already pushed to a shared repository. This is likely to create a lot of confusion.

OTHER TIPS

Most likely nothing was erased. You've probably created a new anonymous branch. If you checkout master, or whatever branch you started from, the missing changes should still be there.

Try git log --all; it should show all the commits in your repository.

There are some git commands that can destroy information, but you probably haven't done so, though it's hard to be sure without knowing just what commands you executed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top