Useful git commands, increase your productivity.
Published:
This post is for keeping useful git commands.
Useful git commands
| Commands | Descriptions |
|---|---|
git clone | create 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 checkout | used 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 pull | used to fetch changes from a remote repository and merge those changes into your current branch. π |
git tag3 | list 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 blame | shows 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 log | shows 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 reflog | records 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 rebase | reapplies 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 diff | shows 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 --amend | Used 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:
HEADis a pointer that points to the latest commit of current branch. IfHEADis detached, it means you are not on any branch. You can see whereHEADpoints to by runningcat .git/HEAD.2: By default, tags are not pushed to remote. Refer to
git pushto push your tags to remote.3: Tags are stored in
.git/refs/tags.
Config
| Config Key | Description |
|---|---|
column.ui auto | Automatically 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
