DEV Community

Angela
Angela

Posted on • Edited 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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post