Git is a handy but complex system. The difficulty, first of all, is that through carelessness you can make a mistake, which is then difficult or even impossible to correct. The Git documentation provides descriptions of many commands to help you fix a bug.
But the thing is, to fix the problem, you need to know the exact name of the command. And here we have the typical chicken and egg problem. This article describes the commands that help you solve problem situations.
Damn, I did something wrong. Give me a magic time machine!
git reflog # you will see a list of every thing you've # done in git, across all branches! # each one has an index HEAD@{index} # find the one before you broke everything git reset HEAD@{index} # magic time machine
This command allows you to recover accidentally deleted data by rolling back the merge that caused the trouble. refLog is used very often - let's say thanks for suggesting to add this command.
I made a commit, but immediately noticed a mistake, it needs to be fixed!
# make your change git add . # or add individual files git commit --amend --no-edit # now your last commit contains that change! # WARNING: never amend public commits
The command makes it possible to correct unpleasant little things - when you committed something, and then saw a problem like a missing space after the "=" sign. Yes, it is possible to make changes with a new commit by combining the two with rebase -i. But it's a long way to go.
NB! Never change commits on a public branch. Use the command only for commits on the local branch, otherwise you will have problems.
I want to change the message of the last commit!
git commit --amend # follow prompts to change the commit message
It's just ... stupid posting requirements.
I accidentally committed to master, although it should have been on a new branch!
# create a new branch from the current state of master git branch some-new-branch-name # remove the last commit from the master branch git reset HEAD~ --hard git checkout some-new-branch-name # your commit lives in this branch now :)
If you've already committed to a public thread, the commands won't work. In this case, git reset HEAD @ {specify the number of commits to revert to} instead of HEAD ~.
Well, I mistakenly committed to the wrong branch
# undo the last commit, but leave the changes available git reset HEAD~ --soft git stash # move to the correct branch git checkout name-of-the-correct-branch git stash pop git add . # or add individual files git commit -m "your message here"; # now your changes are on the correct branch
There is another way that a lot of developers use cherry-pick.
git checkout name-of-the-correct-branch # grab the last commit to master git cherry-pick master # delete it from master git checkout master git reset HEAD~ --hard
I need to run diff, but nothing works.
If you are sure that the changes have been made, but the diff is empty, it may well be that you indexed the changes via add. Therefore it is worth using a special flag.
git diff --staged
In general, this is not a bug, but a feature, but it is damn obvious Β― \ _ (γ) _ / Β―
I urgently need to undo a commit that was made 5 commits ago
# find the commit you need to undo git log # use the arrow keys to scroll up and down in history # once you've found your commit, save the hash git revert [saved hash] # git will create a new commit that undoes that commit # follow prompts to edit the commit message # or just save and commit
Fortunately, you don't have to go back 5 commits by copying and pasting old and new files. You can undo all this with revert.
In addition, you can rollback not only the commit, but the entire file. True, these will already be other commands ...
Undo changes in the file
And here they are, these other commands.
# find a hash for a commit before the file was changed git log # use the arrow keys to scroll up and down in history # once you've found your commit, save the hash git checkout [saved hash] -- path/to/file # the old version of the file will be in your index git commit -m "Wow, you don't have to copy-paste to undo"
When I first found this opportunity, it was COOL, COOL, K-R-U-T-O. But if you think about it - why is checkout the best option for discarding changes in a file? : shakes-fist-at-linus-torvalds:
Everything, I give up
cd .. sudo rm -r fucking-git-repo-dir git clone https://some.github.url/fucking-git-repo-dir.git cd fucking-git-repo-dir
Thanks to Eric V. For this method. And address all complaints about using sudo to him.
If you need to zero out the changes and completely roll back to the original version, then you can try to do just that. But remember - these commands are destructive and irreversible.
# get the lastest state of origin git fetch origin git checkout master git reset --hard origin/master # delete untracked files and directories git clean -d --force # repeat checkout/reset/clean for each borked branch
Attention! This article is not intended to be a comprehensive guide. And yes, there are other ways to do the same, and even better. But I came up with these options by trial and error. Then I had a crazy idea to share my findings. Take it or go!
Expert commentary
Daniil Pilipenko , director of the IT specialist recruitment center at SymbioWay and evangelist of the Skillbox online university backend, added his opinion on Git and its relevance for developers.
Git appeared in 2005, and it took a long time to take over the market. I remember when we were implementing SVN in the development team back in 2008. And even in 2012, a company close to me was heavily implementing Mercurial. Over the years, it has become apparent to many that Git is the best version control system and is now used by almost all developers.
If you're a beginner developer looking to get a job, be sure to learn Git! You should know what a version control system is and why it is needed, what is a commit, a branch, how to clone a repository and send the changes made to the server, how to get new changes from the server, how to merge, what kinds of βresetβ are there. At first, this topic may seem incomprehensible and difficult to you, but you just need to get used to using Git, and you will not be able to wean it.