Git Useful commands – DEV Community

August 8, 2022


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 modeExit fullscreen mode
  • Revert all local uncommitted changes:
git checkout .
Enter fullscreen modeExit fullscreen mode
  • Revert all uncommitted changes:
git reset --hard HEAD
Enter fullscreen modeExit fullscreen mode
  • Remove all local untracked files, so only git tracked files remain:
git clean -fdx
Enter fullscreen modeExit 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 modeExit fullscreen mode
  • Delete the most recent commit, keeping the work you’ve done:
git reset --soft HEAD~1
Enter fullscreen modeExit fullscreen mode
rm -rf .git
Enter fullscreen modeExit fullscreen mode
  • Remove a file from a Git repository without deleting it from the local filesystem
git rm --cached .classpath
Enter fullscreen modeExit 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 modeExit fullscreen mode
  • How to revert to origin’s master branch’s version of file
git checkout origin/master filename
Enter fullscreen modeExit 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 modeExit fullscreen mode

If you want to do the opposite:

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

In pulling

git pull -X theirs
Enter fullscreen modeExit fullscreen mode

In a conflicted state by file

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



Source link

Comments 0

Leave a Reply

Your email address will not be published. Required fields are marked *