DEV Community

Angela
Angela

Posted on • Updated on

Useful Git commands

Log

git log --oneline --graph --all

Revert to Head

If you are not sure of the hash of the last commit, you can go back to the commit before the merge

git reset --hard HEAD~1

To update remote branch:

git push -f

Revert a commit

git reset --hard [commit ID]

To update remote branch:

git push -f

I've used this command to revert to a previous commit after a merging error. It did not removed files that were marked as added.

Revert a single file to a specific version

  • Find the commit ID of the version of the file you want to revert to.
  • Find the path to the file you want to revert from the working directory.
  • In the terminal, change directories to the working directory. -Type:

git checkout [commit ID] -- path/to/file and hit enter.

  • Commit the change to the reverted file.

Changes to be committed:
(use "git restore --staged ..." to unstage)
modified: app/x.py

Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git restore ..." to discard changes in working directory)
modified: app/x.py

Commit empty

git commit --allow-empty -m "[notest]"

Add specific changes
git add --patch

Useful links:

freeCodeCamp undo merge

Top comments (0)