Fixing Git mistakes
More often than not, if protocol is followed and we make sure to take good care of files and repo, things should run smoothly.
Nonetheless, we will face problems and complications along the way, either if working on a very big project or on a shared project with more people.
List everything across every branch
If you are not sure of how bad your mistake was, the priority should be finding that out. Besides the command
git log that outputs all the commits on current branch, there is another command which will list everything you have done at any branch:
Accidentally added file
If you have accidentally added a file to the stanging area, for instance if you have added a lot of changes with a command such as
git add ., you can easily unstage a file with
Terminalgit restore --staged <file>Copiar
One of the best ways of fixing a mistake is not having it in the first place, so here's a great beginner tip for some mistake prevention.
Sometimes there are files we must have in our directory, but do not want to have commited on the final repository or uploaded anywhere in the cloud.This includes files and folder such as
__pycache__/, sensitive information such as passwords and any other thing you want there.
The solution for this is quite easy, just create a file called
.gitignore and list all files you wish to be ignored by Git. These will not figure as untracked files, changes files, etc.
That only works for files that have never been commited to your repo. If you wish to ignore files that were commited to the repo, you must first delete them from it. You can do that with the command:Terminalgit rm --cached <file>Copiar
This will delete the file from the repo, but keep it in your Working Directory.
You can even use an assistant tool to generate a .gitignore for your OS, language, etc.
Amend a commit
If you've just commited something and realize you mised a file or small change, you can fix the previous commit instead of making a new commit. Just add the changes and execute the command:
Terminalgit commit --amend --no-fileCopiar
NEVER amend a public commit, or a commit you have pushed into a public repo. There may be harsh consequeneces. If it was pushed somewhere, suck it up and make a new commit.
Fix commit message
If you wish to fix or change the last commit message, you can also do that with the --amend option:
Terminalgit commit --amend -m "<new_message>"Copiar
Changing further in the past
Sometimes the mistakes we make must it so that we need to change the past, not what we have imediatelly done.
If you wish to undo a particular commit, you can use
git log to find the hash for that commit. The hash is a long sequence of numbers and letters that uniquely identify each commit.
Terminalgit revert <commit_hash>Copiar
and Git will create a new commit undoing the hashed commit.
Revert a single file
If you wish to revert a single file to a previous state, the procedure will be a little different. You'll still have to find and copy the hash for the commit. After that, use the file path on
Terminalgit git checkout <commit_hash> -- <path/to/file>Copiar
Going back in time
Sometimes, though, depending on how hard we have screwed up, reverting a commit might not be enough. Were this to happen, we could try a different solution, such as cancelling a whole lot of commits and reseting back to a previous point. This will make as if the commits after that point had never happened.
There are 3 ways of doing a reset in Git. All of them will change the current branch on your repo back to the commit you chose. The difference is the way each option will treat the changes.
If we use
Terminalgit reset --soft <commit_hash>Copiar
our branch will be restored to that commit, and the changes will be uncommited, they will remain as
That means if we were to do a commit again, we will chave those changes on the repo once again.
Changing soft for
mixed will have a slight change. In this case, the changes will be uncommited and unstaged, leaving them on the working directory only, which means that to be commited once again, you'd have to add them once more before committing.
The third and last method of resetting is the
hard reset, which will uncommit, unstage and delete all changes. This takes your repo back to the commit you chose and permanently deletes all changes that had been done. It is a very delicate operation and should be executed carefully.
As seen previously, git can help us a lot in fixing possible mistakes, but the tools can only take us so far. The best to keep it all safe it practising good measures throughout the development.
Keep the Single Responsability Principle
Make clean, single-purpose commits
Write meaningful and clear commit messages
Use branches, do not commit everything to master
Also, take in account somethings we will se on the next section:
Use a branching methodology