Essential Git Commands Every Developer Should Know

Version control is a vital part of modern software development, and Git is the most widely used system to manage code
changes. Whether you’re a beginner or brushing up your skills, here’s a curated list of important Git commands that
every developer should keep handy.
On this page
Setup and Configuration
Git local configuration
git config --local user.email "username@email.com"
git config --local user.name "User Name"
git config --local credential.helper ""
Checking Your Settings
git config --list
You can fetch all branches from all remotes like this:
git fetch --all
Create a branch in Git from another branch
git checkout -b feature/YOUR_BRANCH_NAME main
How to delete a Git branch locally
git branch --delete YOUR_BRANCH_NAME
How to “pull” from a local branch into another one?
You have to tell Git from where to pull, in this case from the current directory/repository (.):
git pull . master
But when working locally, you can simply use merge (pull internally calls merge):
git merge master
Essential Git Commands with Descriptions
git config --global user.name
Sets the name that will be attached to your commits and tags globally.
git config --global user.email
Sets the email address that will be attached to your commits and tags globally.
git init
Initializes a new Git repository in the current directory.
git add -A
Adds all changes to the staging area, including new, modified, and deleted files.
git clone https://github.com/gauravrjoshi/YOUR_REPO.git
Clones a repository from the given URL to your local machine.
git checkout master
Switches to the master branch in your local repository.
git checkout YOUR_BRANCH_NAME
Switches to the specified branch in your local repository.
git status
Displays the status of files in the working directory and staging area.
git add .
Stages all new and modified files in the current directory.
git commit -m "commit message"
Commits staged changes with a message.
git push origin YOUR_BRANCH_NAME
Pushes local commits to the corresponding branch on the remote.
git pull origin YOUR_BRANCH_NAME
Pulls updates from the remote branch and merges them into your local branch.
git diff
Shows differences between working directory and index, or between commits.
git remote add origin https://github.com/gauravrjoshi/YOUR_REPO.git
Adds a new remote named ‘origin’ to your local repository.
git push origin master
Pushes commits from your local master branch to the remote master branch.
git mergetool
Opens a tool to resolve merge conflicts.
git reflog --all
Displays the history of all references (branches, tags) in your local repository.
git config --local user.name "Your Name"
Sets the commit author name for the current repository only.
git config --list
Displays all Git configurations in effect at the current level.
git fetch --all
Fetches all branches from all remotes without merging.
git checkout -b feature/YOUR_BRANCH_NAME main
Creates a new branch from ‘main’ and switches to it.
git branch --delete YOUR_BRANCH_NAME
Deletes a local branch. Use --force
to force delete if necessary.
git merge master
Merges changes from the master branch into your current branch.
git log -n 5 --oneline
Displays the latest 5 commits in a compact format.
git reset --hard HEAD~1
Removes the last commit and resets the working directory to that state.
git push -f origin sandbox
Force pushes the current branch to ‘sandbox’, overwriting changes.
git clean -n
Previews files that would be deleted by git clean
.
git clean -f
Forcefully deletes untracked files in your working directory.
git count-objects -vH
Shows the number and size of loose objects in the repository.
git checkout target_branch git merge source_branch git commit -m "Merge changes" git push origin target_branch
Steps to merge a branch into another and push the changes.
For more information, visit
Git’s official documentation
.
Command | Description |
---|---|
git config –global user.name | Sets the name that will be attached to your commits and tags globally. |
git config –global user.email | Sets the email address that will be attached to your commits and tags globally. |
git init | Initializes a new Git repository in the current directory. |
git add -A | Adds all changes to the staging area, including new files, modified files, and deleted files. |
git clone https://github.com/gauravrjoshi/YOUR_REPO.git | Clones the repository located at the given URL into a new directory on your local machine. |
git checkout master | Switches to the master branch in your local repository. |
git checkout YOUR_BRANCH_NAME | Switches to the specified branch in your local repository. |
git status | Shows the status of files in the working directory and staging area. |
git add . | Adds all new and changed files in the current directory to the staging area. |
git commit -m “commit message” | Commits the staged changes to the repository with the provided commit message. |
git push origin YOUR_ANOTHER_BRANCH_NAME | Pushes the commits from your local branch to the specified branch on the remote named ‘origin’. |
git pull origin YOUR_BRANCH_NAME | Fetches changes from the specified branch on the remote and merges them into your current branch. |
git pull origin YOUR_ANOTHER_BRANCH_NAME | Fetches changes from another specified branch on the remote and merges them into your current branch. |
git push origin YOUR_BRANCH_NAME | Pushes the commits from your local branch to the same branch on the remote named ‘origin’. |
git diff | Shows the differences between the working directory and the index (or between two commits). |
git remote add first-push-test https://github.com/gauravrjoshi/first-push-test.git | Adds a new remote named ‘first-push-test’ to your local repository. |
git remote add origin https://github.com/gauravrjoshi/first-push-test.git | Adds a new remote named ‘origin’ to your local repository. |
git push origin master | Pushes the commits from your local master branch to the master branch on the remote named ‘origin’. |
git mergetool | Opens a graphical merge conflict resolution tool to resolve merge conflicts. |
git reflog –all | Shows a log of all the references (branches, tags) and their updates in the local repository. |
git config –local user.email “username@email.com” | Sets the email address for your commits in the current repository. |
git config –local user.name “User Name” | Sets the name for your commits in the current repository. |
git config –local credential.helper “” | Removes the credential helper for the current repository, affecting how passwords are stored. |
git config –list | Lists all settings Git can find at that point. |
git fetch –all | Fetches all branches from all remotes. |
git checkout -b feature/YOUR_BRANCH_NAME main | Creates a new branch named ‘feature/YOUR_BRANCH_NAME’ from ‘main’ and checks it out. |
git branch –delete YOUR_BRANCH_NAME | Deletes the specified local branch. Use ‘–delete –force’ if the branch has unmerged changes. |
git pull . master | Pulls changes from the master branch into the current branch within the same repository. |
git merge master | Merges changes from the master branch into the current branch. Useful for local branches without needing to pull. |
git log -n 5 –pretty=format:”%h %ad %s” –date=short | Displays the latest 5 commits in a concise format that includes the commit hash, commit date, and commit message. |
git log -n 5 –oneline | Shows the latest 5 commits with a concise one-line format displaying the commit hash and commit message. |
git reset –hard HEAD~1 | Resets the current branch to the state it was in just before the last commit, discarding any changes. |
git reset –soft HEAD~1 | To undo the last commit while keeping your changes staged for a new commit. |
git push -f origin sandbox | Force-pushes the current branch state to the ‘sandbox’ branch on the remote named ‘origin’, potentially overwriting changes. |
git fetch origin | Fetches updates from the remote named ‘origin’ but does not merge them into the current branch. |
git reset –hard origin/branch-name | Resets the current branch to match the specified branch on the remote named ‘origin’, discarding any local changes. |
git branch -d branch_name | Deletes the specified local branch. Use -D to force deletion if the branch has unmerged changes. |
git clean -n | shows you what would be removed without actually deleting any files |
git clean -f | Command is used to forcefully remove untracked files from your working directory |
git count-objects | We can see the total repository size and how many objects are being used to calculate that size. |
git count-objects -vH | Find the current size of your git repo |
git checkout target_branch git merge source_branch git commit -m “Merge changes from source_branch” git push origin target_branch |
Switches to the target branch, merges changes from the source branch, commits the merge, and pushes the changes to the target branch on the remote named ‘origin’. |
For more information on Git and version control, visit Git’s official documentation. |
Conclusion
Mastering Git isn’t just about memorizing commands—it’s about understanding how code collaboration works. These commands
are the foundation of every successful developer’s toolkit.
Happy coding! 💻✨
