![]() This uses the git rev-list command and -max-parents flag to give you the first commit ID in the history. This trick can also get you to return your branch to the initial commit without losing the other commits, which is sometimes useful: git rebase -i $(git rev-list -max-parents=0 HEAD) You’ll see that the commits are still there, but the master branch has been moved back to the commit you wanted: 8f08673 (rejected-1) Bad change committed If you run the git log command again: git log -oneline -graph -all Put a ‘ d‘ next to the commits you want to forget about. COMMITID=$(git rev-list HEAD | head -3 | tail -1)Īt this point you’re prompted to decide what to do with the previous commits before continuing. Then you use the git rebase command with the -i (interactive) flag to ‘drop’ the relevant commits from your branch. As before, you first get the relevant commit ID. This time we’re going to use git rebase to go back to ‘Change 6’. Now your HEAD pointer and master branch are pointed at the change you wanted. Now check that that change is the one you want: git show $" Let’s say we want to reverse the last-but- two commit on our branch (ie the one that added ‘ Change 9‘ to the file).įirst we use the git rev-list command to list the previous changes in reverse order, and capture the commit ID we want to the LASTBUTONE variable using pipes to head and tail: COMMITID=$(git rev-list HEAD | head -3 | tail -1) Sometimes the manual approach is not easy to achieve, or you want to revert a specific commit (ie not the previous one on your branch). Next, you apply the changes by hand, or you can run this command (which effectively removes the last line of the file) to achieve the same result in this particular context only: head -9 file > file2Īnd then commit the change: git commit -am 'Revert C10 manually' ![]() If you haven’t done any funny business on your repo then it points to the last commit on the branch you are working on. The HEAD tag always points to a specific commit. The output should show you a diff of what changed in the last entry. ![]() The ‘ git show‘ command can give you this: git show HEAD The simplest way (conceptually, at least) to undo a change is to add a new commit that just reverses the change you last made.įirst, you need to see exactly what the change was. See here if you want to know more about this git log command. ![]() If you run: git log -oneline -graph -allĪt any point in this walkthrough you should be able to see what’s going on. Now you are in a fresh folder with a git repository that has 10 simple changes to a single file (called ‘ file') in it. If you want to follow along, run these commands to set up a simple series of changes to a git repository: cd $(mktemp -d) There’s many ways to achieve the result you might want, so this can be a pretty instructive and fertile question to answer. This got me thinking, and I came up with a walkthrough similar to the ones I use in my book to help embed key Git concepts and principles. Recently, while showing someone at work a useful Git ‘trick’, I was asked “how many ways are there to undo a bad change in Git?”. ![]()
0 Comments
Leave a Reply. |