#GIT REVERT COMMIT HOW TO#
I told her that I had read all these articles and watched videos about how to use git correctly. A fellow work colleague gave me some great advice about learning git. Unfortunately, the best method to understand how it operates is to have a thorough understanding of how the staging, indexing, and the remote repository work in conjunction. Maybe I have been a solo developer for too long! Conclusion Basically, it is bad practice to force a push on a public git repository, and you should use revert instead. Please be aware that some of these techniques specified in this post to undo git commits and pushes are bad practices for public git repositories… especially when working with multiple developers. The -force argument will delete previous commits and push the current local git repository. The basic command to undo the last commit is: If you have not pushed the repository yet (say to GitHub) this is the method to use to undo the last commit you made. We have not discussed this so far, but a git commit is greatly different from a git commit followed by a git push. Please note the list displayed by the git log command is from most recent commit to the oldest commit.
The commit message (e.g., Fourth commit: task4.py and Third commit: task3.py).We can still use this partial object name and do not require the complete name. This value is actually 40 characters in length, however, the git log -oneline command only displays a partial prefix. The output from the git log command is a summary of what has been committed. Only one command is needed, and it is irrelevant which method you used to undo the original commit:Ħf81b42 (HEAD -> master) Fourth commit: task4.py Once you have fixed the git index you can now fix the GitHub repository. Undo a specific commit and remove the changes (from disk): Undo a specific commit and do not remove the changes: Undo the last commit and remove the changes (from disk): Undo the last commit and do not remove the changes: If you want a quick answer, here are the following commands to undo a git commit: Hopefully, this post is useful to others as it was for me to document. This post will discuss how to undo unfortunate git commits… as well as git pushes. The knowledge of how to do it was never achieved, mainly because I barely used this feature. I then performed a revert from a git push to my GitHub. I was asking myself this question: how can I recover from an accidental git commit or git push? Which is basically summarised as: how to undo commits or pushes in git? After reading a few forum posts, riding the waves of stack overflow answers… I started to get it.