Here are the list of most used 40 Git command that a developer mostly used in a day-to-day coding.
is a distributed version control system that helps developers collaborate on projects of any scale.
Git is a distributed version control system that helps developers collaborate on projects of any scale. As a developer you should know following commands which might speedup your day-to-day coding activity.
1. git config
#
Command
Description
1
git config --global user.name <name>
Define the author name to be used for all commits by the current user.
2
git config --global user.email <email>
Define the author email to be used for all commits by the current user.
Delete all branch from local except main and sandbox branch.
3. git diff
#
Command
Description
1
git diff HEAD
Show difference between working directory and last commit.
2
git diff --cached
Show difference between staged changes and last commit.
4. git log
#
Command
Description
1
git log -<limit>
Limit number of commits by <limit>. E.g. git log -5 will limit to 5 commits.
2
git log --oneline
Condense each commit to a single line.
3
git log -p
Display the full diff of each commit.
4
git log --stat
Include which files were altered and the relative number of lines that were added or deleted from each of them.
5
git log --author= "<pattern>"
Search for commits by a particular author.
6
git log --grep="<pattern>"
Search for commits with a commit message that matches «pattern>.
7
git log <since> .. <until>
Show commits that occur between <since> and <until>. Args can be a commit ID, branch name, HEAD, or any other kind of revision reference.
8
git log -- <file>
Only display commits that have the specified file.
9
git log --graph --decorate
--graph flag draws a text based graph of commits on left side of commit msgs. --decorate adds names of branches or tags of commits shown.
5. git rebase
#
Command
Description
1
git rebase -i <base>
Interactively rebase current branch onto <base>. Launches editor to enter commands for how each commit will be transferred to the new base.
6. git pull
#
Command
Description
1
git pull <remote>
It is used to fetch and download content from a remote repository and immediately update the local repository to match that content.
2
git pull --rebase <remote>
Fetch the remote's copy of current branch and rebases it into the local copy. Uses git rebase instead of merge to integrate the branches.
7. git push
#
Command
Description
1
git push <remote> --force
Forces the git push even if it results in a non-fast-forward merge. Do not use the -force flag unless you're absolutely sure you know what you're doing.
2
git push <remote> --all
Push all of your local branches to the specified remote.
3
git push <remote> --tags
Tags aren't automatically pushed when you push a branch or use the -all flag. The -tags flag sends all of your local tags to the remote repo.
8. git remote
#
Command
Description
1
git remote -v
To see all remote repositories for your local repository.
2
git remote add origin <URL>
Add a new remote URL
3
git remote set-url origin <URL>
To changes an existing remote repository URL.
9. git merge
#
Command
Description
1
git merge —squash <branch | commit>
Performs a squash merge
2
git merge <branch | commit>
Merge a specific branch or commit into current branch
3
git merge --abort
Aborts the merge
10. git reset
#
Command
Description
1
git reset
Reset staging area to match most recent commit, but leave the working directory unchanged.
2
git reset <commit>
Move the current branch tip backward to <commit>, reset the staging area to match, but leave the working directory alone.
3
git reset --hard
Reset staging area and working directory to match most recent commit and overwrites all changes in the working directory.
4
git reset --hard <commit>
Same as previous, but resets both the staging area & working directory to match. Deletes uncommitted changes, and all commits after <commit>.
11. Miscellaneous
#
Command
Description
1
git shortlog -sn --email
Get commit count by user's email
2
git fetch --prune --all
To fetch all the branch from remote and also remove any remote-tracking references that no longer exist on the remote
Let me know in the comments out of the 40 commands which are the one that you are using the most, and you can also suggest me some handy command to add to the above list.
Along with the above commands I have also customise my git configuration with aliases. If you want to know then checkout my .gitconfig file in dotfiles GitHub repository.
I'm Raunak Gupta, a seasoned software developer with over 9 years of experience in a wide range of programming languages, frameworks, and tools. I started my journey as a WordPress & CakePHP developer in 2014, diving deep into the world of OOPs, Request handling, and SEO. Along the way, I crafted numerous dazzling WooCommerce stores, tamed payment gateways, optimized for full filament functionality, and achieved ultra-low latency for lightning-fast load times. My expertise extends to BI tools, website builders, DevOps, and team leadership. I like to help upcoming developers, so I share my experience through this blog and by assisting fellow developers on Stack Overflow, where I've earned a stellar reputation with over 10k+ points of recognition.
Cheers, mate! I’d also recommend giving “git reset –hard origin/master” a go. It’s saved me loads of times, you know?
Some truly nice hack, I like it.