Sh#t, I goofed on Git!
Inspecting Commit History
The git log command is used to inspect the commit history of a repository. By default, it will show the commit
history for the current branch. You can also specify a branch or commit to view the history for that branch or commit.
A quick way to see the commit history of multiple branches is to use the --graph flag. This will show the commit
history for all branches in a tree-like structure. The --pretty=oneline flag will show the commit history in a
single line per commit. The --abbrev-commit flag will abbreviate the commit hash to the first 7 characters.
A common use of git log is to view the commit history for a specific file. This can be done by specifying the file
name after the git log command.
Undo and Redo A Local Commit
Let's say you've accidentally committed something to the wrong branch, but you caught it before you pushed it to the remote.
You can undo the commit and redo it on the correct branch. The git reset command is used to reset the HEAD to a
specified state, while leaving your local files in their current state. HEAD~ references one commit before the
previous. Together, this command asks git to reset to one commit before your previous, effectively undoing the local
commit.
You can now make edits to your files, change branches, or do whatever you need to do to get your local files in the
correct state. Once you're ready to redo the commit, you can use the git commit command as normal.
Undoing Multiple Local Commits
If you happened to make multiple commits before pushing your changes to the remote, you can still use the git reset
command to undo them. Instead of using the HEAD~ syntax, you can use HEAD~<number of commits to undo>. For example,
if you wanted to undo the last 3 commits, you would use HEAD~3.
Complete Example
git commit -m "I don't know this yet, but I goofed by committing this."
git reset HEAD~
# Make edits to files, change branches, etc.
git add <files>
git commit -m "I figured out my goof and now I'm committing things correctly."
Undo and Redo A Pushed Commit
Let's say you've accidentally committed something you didn't want to, and you pushed it to the remote. You can undo
fix this in multiple ways. The first way is to use the git revert command. This command creates a new commit that
reverts the changes in the specified commit. This is useful in that you won't create problems for anyone who has
already pulled the changes you made. However, it does create a new commit, which can be confusing if you're trying to
keep your commit history clean.
The second way is to use the git reset command. This command is similar to the git reset command used to undo
local commits, but it has a few differences. First, it uses the --hard flag to reset the HEAD to a specified state,
and also reset the working tree to match. This means that any changes you made to your local files will be undone.
Since this command is destructive, it's important to be careful when using it. If you're not sure what you're doing,
it is recommended that you use the git revert command instead.
Once you have undone the commit, you will need to force push the changes to the remote. This is because you are
rewriting history, which is not allowed by default. You can force push by using the -f flag with the git push
command.
More Information
For more information, see the documentation for: - git reset, - git revert, - git log.
You can also check the built-in help documentation for git:
Thank you, Stack Overflow for the examples and pointers! - https://stackoverflow.com/questions/927358/how-do-i-undo-the-most-recent-local-commits-in-git - https://stackoverflow.com/questions/2529971/what-is-the-head-in-git/46350644#46350644 - https://stackoverflow.com/questions/1064361/unable-to-show-a-git-tree-in-terminal - https://stackoverflow.com/questions/22682870/how-can-i-undo-pushed-commits-using-git