Useful git commands, increase your productivity.

4 minute read

Published:

This post is for keeping useful git commands.


Useful git commands

CommandsDescriptions
git clonecreate a clone of a remote Git repository onto your local machine
🎈--recursive: If the repository contains submodules, this option initializes and clones them as well.
🎈--branch <branch>: Clone a specific branch instead of the default branch.
🎈--quiet: Reduce the output during the cloning process.
🎈--no-remote: skips the creation of a remote reference to the original repository, making the cloned repository independent of the remote.
git checkoutused for switching between different branches, restoring files, and creating new branches.
🎈<branch-name>: switch from one branch to another.
🎈-- <file>: restore a file (or multiple files) to its state in the latest commit on your current branch. This is helpful if you have modified a file but want to discard your changes.
🎈<commit-hash>: go to a specific commit. Your git will enter detached HEAD state.1
🎈<commit-hash> -- <file-path>: checkout a specific file from a specific commit. This does not detach your HEAD.1
🎈<branch-name> -- <file>: checkout individual files from another branch
🎈 -b <new-branch-name>: create a new branch and switch to the new branch.
git push🎈<remote> <tagname> push a specific tag
🎈--tags: push all tags
🎈--force: Overwrites the remote branch with your local branch, potentially losing commits on the remote. Use with extreme caution.
🎈--force-with-lease: A safer alternative to --force. It only overwrites the remote branch if your local branch is based on the same commit as the remote branch, preventing accidental overwrites if someone else has pushed changes.
git pullused to fetch changes from a remote repository and merge those changes into your current branch.
🎈
git tag3list all tags
🎈<tagname> Creates a tag to the current commit.2
🎈-a <tagname> -m "message": Creates a tag with a message2
🎈<tagname> <commit-hash>: tag a specific commit2
🎈
git blameshows who last modified each line of a file, along with the commit hash and timestamp.
🎈<file>: The file to inspect.
🎈-L <start>,<end>: Restrict the blame output to the given line range.
🎈-e: Show author’s email instead of name.
🎈git blame -w -C -C -C -L 15,26 <file>: A more advanced example. This command will find the original author of lines 15-26 in the specified file, with the following behavior:
Β Β Β Β -w: Ignores whitespace-only changes.
Β Β Β Β -C -C -C: This is the most aggressive search for finding the origin of moved or copied code, even from different files across different commits.
In short, it’s a powerful way to trace the true origin of a specific block of code.
git logshows the commit history of the current branch.
🎈-p: Show the patch introduced with each commit.
🎈--oneline: Show the commit history in a condensed one-line format.
🎈--graph: Display the commit history as a text-based graph.
🎈-L <start>,<end>:<file>: Trace the evolution of a specific line range within a file, showing commits that changed that code block.
git reflogrecords every change to HEAD (and other references) in your local repository, allowing you to recover lost commits.
🎈show: (default) Shows the reflog entries.
🎈--all: Shows reflog entries for all branches and other references.
git rebasereapplies commits from one branch onto another, creating a linear history. Useful for cleaning up commit history before merging.
🎈<base>: Rebase the current branch onto <base>.
🎈--onto <newbase> <oldbase> <branch>: Rebase a specific range of commits onto a new base.

Interactive Rebase (git rebase -i)
Performs an interactive rebase, allowing you to modify individual commits (e.g., squash, reword, reorder, delete) during the rebase process.
🎈<base>: Interactively rebase commits up to <base>.
Note: This command rewrites history and should be used with caution, especially on shared branches.
git diffshows changes between commits, working tree, etc.
🎈--word-diff: Shows changes on a word-by-word basis, highlighting individual words that have changed within a line. Useful for text documents or code with minor inline edits.
git commit --amendUsed to modify the most recent commit. You can change the commit message, add/remove files, or combine multiple small changes into one commit.
Note: This command rewrites history and should be used with caution, especially on shared branches.
  • 1: HEAD is a pointer that points to the latest commit of current branch. If HEAD is detached, it means you are not on any branch. You can see where HEAD points to by running cat .git/HEAD.

  • 2: By default, tags are not pushed to remote. Refer to git push to push your tags to remote.

  • 3: Tags are stored in .git/refs/tags.


Config

Config KeyDescription
column.ui autoAutomatically enables columnar display for git status, git branch, git tag, and other commands when the output is going to a terminal. This makes the output more readable by aligning columns.
To set this globally: git config --global column.ui auto

References

[1] git-scm