Git Useful commands

Jorge Tovar - Aug 8 '22 - - Dev Community

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
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player