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 focus…


This content originally appeared on DEV Community and was authored by Pratap Sharma

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

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

$ git reset --soft HEAD~1

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

$ git log
$ git reset --hard <commit_id>

$ git push origin <branch_name> -f

How to undo a public commit?

You can just run

$ git revert HEAD

How to delete a Git branch locally and remotely?

  • Deleting local branch
$ git branch -d <branchname>
  • Deleting remote branch
$ git push origin --delete <branch>

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

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

$ git revert <commit-id>

How Undo a git stash?

You can just run:

$ git stash pop

and it will unstash your changes.

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

$ git stash apply --index

How to undo 'git add' before commit?

You can undo git add before commit with

$ git reset <file>

To unstage all due changes

$ git reset

How to rename a local Git branch?

$ git branch -m <oldname> <newname>
  • To rename current branch
$ git branch -m <newname>

How to discard unstaged changes in Git?

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

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

and then delete the local copies of tag:

$ git tag | xargs git tag -d

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.


This content originally appeared on DEV Community and was authored by Pratap Sharma


Print Share Comment Cite Upload Translate Updates
APA

Pratap Sharma | Sciencx (2021-06-11T02:41:30+00:00) Useful Git commands that may save your life. Retrieved from https://www.scien.cx/2021/06/11/useful-git-commands-that-may-save-your-life/

MLA
" » Useful Git commands that may save your life." Pratap Sharma | Sciencx - Friday June 11, 2021, https://www.scien.cx/2021/06/11/useful-git-commands-that-may-save-your-life/
HARVARD
Pratap Sharma | Sciencx Friday June 11, 2021 » Useful Git commands that may save your life., viewed ,<https://www.scien.cx/2021/06/11/useful-git-commands-that-may-save-your-life/>
VANCOUVER
Pratap Sharma | Sciencx - » Useful Git commands that may save your life. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/11/useful-git-commands-that-may-save-your-life/
CHICAGO
" » Useful Git commands that may save your life." Pratap Sharma | Sciencx - Accessed . https://www.scien.cx/2021/06/11/useful-git-commands-that-may-save-your-life/
IEEE
" » Useful Git commands that may save your life." Pratap Sharma | Sciencx [Online]. Available: https://www.scien.cx/2021/06/11/useful-git-commands-that-may-save-your-life/. [Accessed: ]
rf:citation
» Useful Git commands that may save your life | Pratap Sharma | Sciencx | https://www.scien.cx/2021/06/11/useful-git-commands-that-may-save-your-life/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.