DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Git Useful commands
Jorge Tovar
Jorge Tovar

Posted on

Git Useful commands

Git useful commands

Our brain is better at processing data than storing it. So that's why I'm creating this document with one of the useful but maybe not so common commands to get the best for this wonderful tool

  • Unstage all files you might have staged with git add
git reset
Enter fullscreen mode Exit fullscreen mode
  • Revert all local uncommitted changes:
git checkout .
Enter fullscreen mode Exit fullscreen mode
  • Revert all uncommitted changes:
git reset --hard HEAD
Enter fullscreen mode Exit fullscreen mode
  • Remove all local untracked files, so only git tracked files remain:
git clean -fdx
Enter fullscreen mode Exit fullscreen mode

WARNING: -x will also remove all ignored files, including ones specified by .gitignore! You may want to use -n for a preview of files to be deleted.

  • Delete all branches except the main one
git branch | grep -v "main" | grep -v "master" | xargs git branch -D
Enter fullscreen mode Exit fullscreen mode
  • Delete the most recent commit, keeping the work you've done:
git reset --soft HEAD~1
Enter fullscreen mode Exit fullscreen mode
  • Remove .git tracking
rm -rf .git
Enter fullscreen mode Exit fullscreen mode
  • Remove a file from a Git repository without deleting it from the local filesystem
git rm --cached .classpath
Enter fullscreen mode Exit fullscreen mode
  • If you have a sequence of commits

... - Commit1 - Commit2 - ... Commit5 <- HEAD
To squash Commit2 to Commit5 into a single commit, you can reset your branch to Commit1 and then commit again:

git reset --soft Commit1
git commit
Enter fullscreen mode Exit fullscreen mode
  • How to revert to origin's master branch's version of file
git checkout origin/master filename
Enter fullscreen mode Exit fullscreen mode
  • Resolve Git merge conflicts in favor of their changes during a pull In a conflicted state, and you want to just accept all of theirs:
git checkout --theirs .
git add .
Enter fullscreen mode Exit fullscreen mode

If you want to do the opposite:

git checkout --ours .
git add .
Enter fullscreen mode Exit fullscreen mode

In pulling

git pull -X theirs
Enter fullscreen mode Exit fullscreen mode

In a conflicted state by file

git checkout --theirs path/to/file
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
marblewraith profile image
Matthew Rath

git reset --hard HEAD

Using it in this way is a bad idea. As you say it completely destroys all uncommitted changes in the working directory with no way to undo it. I suggest adding and using the following alias instead:

qWipe = '!git add -A && git commit -qm "SAVEPOINT" && git reset HEAD~1 --hard'

This will:

  1. Create a commit of your current working directory with the subject line "SAVEPOINT"
  2. Then do a reset hard, back to where you were originally.

This creates a "hanging commit" and if nothing else happens it'll be garbage collected in due course (i think it's about a month IIRC by default).

But should you ever need to need to recover any of the changes you had before, that hanging commit can still be reached via the reflog.

🌚 Browsing with dark mode makes you a better developer.

It's a scientific fact.