GIT – USEFUL COMMANDS
In some cases, you will need to face issues on your code. Those are mostly the bests moments to exploit the functionalities of our version control system.
Below, i’m going to post many useful commands that would help you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# Return to 10(or the number you want) commits before. git reset --hard HEAD~10 # Then, to restore actual version, you just need to do: git pull origin <branch> # How to remove everything you did after last pull. # This include tracked and untracked files. git stash -u # If you need to change between branches, or just keep your # changes on a side for a moment, the stash its pretty useful. // Put your tracked code on the stash(it works like a heap). git stash // Get your code from the stash (get all changes on LIFO order). git stash pop |
If you made a commit BUT NOT PUSHED IT, and you need to go back for any reason, you can do the following:
1 2 3 4 5 6 7 8 9 10 11 12 |
git commit -m "this is the commit that i will need to restore" # undo last commit before push git reset HEAD~ # remove last commit,and added files will be shown again. git reset HEAD~1 # --- edit files as necessary --- git add /paths/to/files git commit -m "my real commit" |
If you made a commit AND PUSHED IT, and you need to go back for any reason, you can do the following:
1 2 3 4 5 |
git commit -m "this is the commit that i will need to restore" git push <remote> <branch> git revert HEAD # This command will revert/remove the last one commit/change and then you can push |
If you made a commit, and you need to remove a file from there, you can do the following:
1 2 3 4 5 6 7 8 |
git commit -m "this is the commit that i will need to change" git reset --soft HEAD~1 #Instead, you can do: git reset --soft HEAD^ git reset HEAD /path/to/file/you/want/to/remove/from/commit git commit -m "this is my real commit" |