DEV Community

Cover image for Useful Git commands that may save your life
Pratap Sharma
Pratap Sharma

Posted on • Edited on • Originally published at pratapsharma.io

47 5

Useful Git commands that may save your life

Git commands aren't always easy. If they were, we would have these commands at our disposal. They would be super helpful for performing everyday tasks like creating or renaming a git branch, removing files, and undoing changes.

In this post, I’m focusing on major git commands that gets all (almost) your work fulfilled.

How to undo the most recent local commits in Git?

You can run

$ git reset HEAD~1
Enter fullscreen mode Exit fullscreen mode

You can even undo your commit but leave your files and your index:

$ git reset --soft HEAD~1
Enter fullscreen mode Exit fullscreen mode

If you want to permanently undo the commit and you have cloned some repository:
To get commit id:

$ git log
Enter fullscreen mode Exit fullscreen mode
$ git reset --hard <commit_id>

$ git push origin <branch_name> -f
Enter fullscreen mode Exit fullscreen mode

How to undo a public commit?

You can just run

$ git revert HEAD
Enter fullscreen mode Exit fullscreen mode

How to delete a Git branch locally and remotely?

  • Deleting local branch
$ git branch -d <branchname>
Enter fullscreen mode Exit fullscreen mode
  • Deleting remote branch
$ git push origin --delete <branch>
Enter fullscreen mode Exit fullscreen mode

How to revert to a particular commit?

To revert to a particular commit you'll need a commit id. To get a commit id just run

$ git log
Enter fullscreen mode Exit fullscreen mode

Then copy the commit id you want to rever back to. And then run:

$ git revert <commit-id>
Enter fullscreen mode Exit fullscreen mode

How Undo a git stash?

You can just run:

$ git stash pop
Enter fullscreen mode Exit fullscreen mode

and it will unstash your changes.

In case you want to preserve the state of files (staged vs. working), use

$ git stash apply --index
Enter fullscreen mode Exit fullscreen mode

How to undo 'git add' before commit?

You can undo git add before commit with

$ git reset <file>
Enter fullscreen mode Exit fullscreen mode

To unstage all due changes

$ git reset
Enter fullscreen mode Exit fullscreen mode

How to rename a local Git branch?

$ git branch -m <oldname> <newname>
Enter fullscreen mode Exit fullscreen mode
  • To rename current branch
$ git branch -m <newname>
Enter fullscreen mode Exit fullscreen mode

How to discard unstaged changes in Git?

  • For all unstaged files
$ git checkout -- .
Enter fullscreen mode Exit fullscreen mode
  • For a specific file
git checkout -- path/to/file/to/revert
Enter fullscreen mode Exit fullscreen mode

Note: -- here to remove argument ambiguation.

How to delete all tags from a Git repository?

To delete remote tags simply do:

$ git tag -l | xargs -n 1 git push --delete origin
Enter fullscreen mode Exit fullscreen mode

and then delete the local copies of tag:

$ git tag | xargs git tag -d
Enter fullscreen mode Exit fullscreen mode

Conclusion

I hope this post saved your time and life. If you liked the post, feel free to share it to help others find it!

You may also want to read Getting Started with Git - A beginner's guide

You may also follow me on LinkedIn and Twitter.

💌 If you’d like to receive more tutorials in your inbox, you can sign up for the newsletter here.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (4)

Collapse
 
drsimplegraffiti profile image
Abayomi Ogunnusi

I just saved this....thanks

Collapse
 
pratap2210 profile image
Pratap Sharma

I'm glad. Thanks

Collapse
 
crabsgamer profile image
Crabs

Thank you I really need this

Collapse
 
pratap2210 profile image
Pratap Sharma

I'm glad. Thank you

Image of Bright Data

Maintain Seamless Data Collection – No more rotating IPs or server bans.

Avoid detection with our dynamic IP solutions. Perfect for continuous data scraping without interruptions.

Avoid Detection

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay