
As a professional developer, mastering Git is a must in order to optimize your workflows and stand out. With distributed version control and powerful branching capabilities, Git enables developers to collaborate efficiently at scale.
In this comprehensive guide, I‘ll share the 32 essential Git commands you need to know along with practical examples and tips from my experience as a lead data engineer. Whether you‘re just getting started with Git and GitHub or need a quick refresher, this cheat sheet will level up your source control skills. Let‘s dive in!
Why Git is Essential for Developers
Before we look at the commands, let‘s discuss why learning Git needs to be on every developer‘s roadmap in 2025.
Git has become the dominant version control system for software teams for good reason. According to the latest Stack Overflow developer survey, over 90% of developers now use Git making it a mandatory skill to master.
Here are some of the key reasons Git is so essential:
-
Collaboration at scale – Git enables seamless collaboration within large distributed teams of developers. Changes can be easily merged through built-in controls.
-
Integrity – All changes in Git are secured with a cryptographic hash. This ensures traceability and integrity as code evolves.
-
Experiment safely – Developers can try out features and ideas in isolated branches without impacting the main codebase.
-
Undo mistakes – Since Git tracks the entire history, mistakes can be reverted easily. Code can be rolled back to any previous known good state.
-
Non-linear development – With cheap branching and merging, developers don‘t have to work in a strictly linear way. Context switching is easy.
-
Process efficiency – Git streamlines the entire development workflow from code to test to deploy. Changes can be reviewed, approved and merged smoothly.
According to a survey from Atlassian, development teams that use Git are able to deploy code 21x more frequently than those that don‘t use version control. Git enables teams to ship faster.
Simply put, mastering Git makes you a more productive and effective developer. These commands will help you unlock the full potential of Git.
Git vs GitHub
Before we jump into the commands, it‘s important to clarify the difference between Git and GitHub.
Git is the underlying source control technology that enables version control capabilities locally on your computer.
GitHub is a cloud hosted service built around Git. It provides graphical user interfaces, collaboration tools and cloud hosting for Git repositories.
Git operates on the command line whereas GitHub provides a web interface to interact with repos. While GitHub relies on Git under the hood, you don‘t need GitHub to use Git for version control on your local machine.
Now let‘s explore how to leverage the power of Git.
Essential Git Commands
1. git config
The git config command allows setting configuration values for your Git installation globally or on a per project basis.
git config --global user.name "John"
git config --global user.email "[email protected]"
This associates commit history with your details across all repos on your system.
git config user.name "John"
Sets the user name to use for the current Git repository. Useful for setting this on a per project basis.
Other useful configurations include:
-
git config --global core.editor <editor>– set default text editor -
git config --global alias.<handle> <command>– set up shortcuts for common Git commands
2. git init
The git init command turns a directory into a new Git repository. This is the first step in creating a new repo.
git init my-repo
This creates a .git subdirectory in my-repo which contains all the objects/refs required for the new repository.
3. git clone
git clone creates a local copy of a Git repository that already exists remotely. This is the easiest way to obtain an existing repo to start contributing to.
git clone https://github.com/user/repo.git
This downloads the entire history and codebase from the remote repo. Any changes you commit locally can be pushed back to update the central project repo.
4. git add
The git add command adds new or changed files into the staging area. This stages the changes to be included in the next commit.
git add <file1> <file2>
Only changes that are staged will be committed as a snapshot. Staging allows reviewing changes before committing using git status.
5. git commit
git commit commits the staged changes to the local repository. A commit message is required to summarize the updates.
git commit -m "Implement new feature X"
Commit messages should describe the why of changes rather than just the what. They help document the reason for updates over time.
6. git status
git status displays the state of the working directory and staging area.
git status
This shows what changes are staged, unstaged, and untracked. Running status before add/commit ensures you don‘t miss anything.
7. git log
git log shows the commit history of the current branch.
git log
This displays metadata like commit hash, author, date and message for each commit in reverse chronological order.
git log -p
Using the -p flag shows the full diff of each commit. This is useful to analyze how code has changed over time.
8. git diff
git diff displays unstaged changes between your working directory and the repo.
git diff
This shows file differences not yet staged for commit. diff helps review changes before using add and commit.
git diff --staged
Passing --staged shows changes between staged changes and the last commit. This allows reviewing before committing.
9. git branch
The git branch command manages branches – isolated environments to work on features or ideas.
git branch new-feature
This creates a new branch called new-feature. The files in your working directory remain unchanged.
git branch
Running just git branch lists out all existing branches allowing you to switch contexts.
git branch -d old-feature
The -d flag deletes the branch specified. This helps clean up old branches that are no longer needed.
10. git checkout
git checkoutswitches context by changing branches (or restoring files from previous commits).
git checkout new-feature
This changes the files in your working directory to match the new-feature branch. Uncomitted local changes may be lost.
git checkout -b bug-fix
The -b flag creates and checks out a new branch called bug-fix in one step.
11. git merge
git merge joins together branches by incorporating changes. This is used to combine locally developed features with the main codebase.
git checkout main
git merge new-feature
This merges the changes made in new-feature into the main branch to publish the changes. If no unique commits exist in the source branch, a fast-forward merge is performed.
12. git tag
git tag assigns human-readable labels to commits to mark release points like v1.0.0.
git tag v1.0.0
This tags the last commit of the current branch with a tag v1.0.0.
git tag -a v2.0.0 -m ‘Version 2.0.0‘
The -a flag creates an annotated tag with the provided release message.
13. git fetch
git fetch downloads the latest commits from a remote repository.
git fetch origin
This pulls down new commits from the origin remote repository without merging them into your local branches.
git fetch upstream bug-123
Here we fetch a specific branch (bug-123) from the upstream remote into the local repo.
git fetch helps update your local view of shared repositories without altering your branches.
14. git pull
git pull fetches remote changes and merges them into the current local branch. This helps keep your local work synchronized with shared repositories.
git pull origin main
This directly pulls main branch changes from origin and merges with the local repo main branch.
git pull --rebase origin main
Passing --rebase applies the changes linearly without creating separate merge commits. This keeps history clean.
15. git push
git push publishes local commits to a remote repo making them accessible to other developers.
git push origin main
This pushes the main branch to origin. Any new commits from the local branch will now become available on the remote.
16. git stash
git stash temporarily shelves changes that aren‘t ready to commit.
git stash
This stashes your changes from the working directory to be reapplied later. This cleans your working copy to switch contexts.
git stash pop
git stash pop reapplies your most recently stashed changes committing them to your working directory.
17. git clean
git clean removes untracked files from your working directory.
git clean -n
The -n flag performs a "dry run" to preview what files would be removed without actually deleting anything.
git clean -f
This forcefully removes any files not tracked by Git from the current directory. Useful for wiping unused build artifacts etc.
18. git rebase
git rebase replays local commits on top of the target base branch. This can help linearize the commit history.
git rebase main
This takes all commits from the current branch and applies them after the last commit on main.
git rebase -i HEAD~5
Passing -i for interactive starts an editor to manually re-order, squash, or drop commits. This can help clean up history.
19. git reset
git reset erases commits by resetting the current branch to a previous state.
git reset HEAD^
This moves the current branch to the HEAD position of the previous commit, undoing the last commit.
git reset --hard HEAD~5
Specifying --hard reverts the files in the working tree to match the target commit. This undoes latest 5 commits and all changes!
20. git rm
git rm removes files from version control while keeping them locally.
git rm filename.txt
This stages the filename.txt file for removal from version control. The file will be kept locally but no longer tracked.
21. git restore
git restore restores files from the Git database to match a previous version.
git restore filename.txt
This overwrites the file in the working directory with the version from Git index. Useful for undoing local edits that have not been staged.
22. git revert
git revert creates a new commit that reverses previous changes.
git revert HEAD
This will generate a new commit reversing the changes made in the last commit. The old commit history will be preserved.
23. git archive
git archive exports a snapshot of files recorded in Git as a tar/zip archive.
git archive --format zip HEAD -o latest.zip
This creates a zipped archive of the HEAD commit and saves it as latest.zip. Useful for creating release packages.
24. git prune
git prune cleans up stored objects that are no longer accessible.
git prune --expire=90
This removes loose objects and unnecessary files that are older than 90 days. Helpful for freeing disk space.
25. git fsck
git fsck verifies the health of the Git database and file system.
git fsck
Running this periodically checks for inconsistencies or corrupted objects in the Git database. This can help identify repository issues early.
26. git reflog
git reflog displays changes to the local repository‘s HEAD reference pointer.
git reflog
This shows a history of changes to the current branch reference including resets, reverts, etc. The reflog persists even after commits are removed from history.
27. git blame
git blame displays author and last modification info per file line.
git blame index.js
This prints out the author and commit details for each line in index.js. Useful for identifying who made what code changes when.
28. git stash list
git stash list displays a summary of all stashed changesets.
git stash list
This lists all stashed changes with the stash number, latest stash at top. Helps identify and reapply specific stashed changes.
29. git cherry-pick
git cherry-pick applies changes introduced by existing commits.
git cherry-pick 230459sd
This picks commit 230459sd and tries to reapply the change it introduced to the current branch. This can help transfer commits between branches.
30. git rebase –onto
git rebase --onto extracts a branch and replays it back onto a new base. Useful for cleaning up history.
git rebase --onto main server client
This takes all the commits that exist in client but not in server and replays them onto main. The client branch is then reset to match main.
31. git worktree
git worktree manages multiple working directories tied to the same repository.
git worktree add -b new-branch ../new-dir
This adds a new worktree for new-branch in ../new-dir where you can make commits isolated from the main working directory. Useful for working on multiple features in parallel.
32. git commit –amend
git commit --amend alters the most recent commit allowing you to add changes or edit the commit message.
git commit --amend
After forgetting to add changes, this updates the last commit with any newly staged changes without creating a new one.
Level Up Your Git Game
These commands just scratch the surface of Git‘s powerful capabilities. Here are some additional tips to go from beginner to Git pro:
-
Leverage tab auto-completion on branch names, file paths etc.
-
Review Git commit history visually with
git log --graph --all --decorate. -
Explore experimental features and undo mistakes with the reflog via
git reflog. -
Enable convenient aliases in your config e.g.
git config --global alias.lol "log --graph --decorate --oneline" -
Fix mistakes with
git commit --amendandgit rebase -i. Rebase lets you edit history interactively! -
Use
git stashto switch contexts when needed but avoid stashing for too long. -
Tag releases and verify signatures (e.g. with
git tag -v) to assure authenticity. -
Perform efficient targeted deletions with
git rm --cached. -
Leverage
git worktreeto manage multiple working directories for complex features. -
Run
git gcandgit fsckperiodically to clean up and verify repository health. -
Use shallow cloning via
git clone --depthfor quicker clones of recent history. -
For advanced branch cleanup, harness the power of
git rebase --onto.
With this guide, you should now have a solid grasp of Git‘s most essential commands for practical version control and collaboration. Git skills are a must-have for any professional developer today. I hope these tips help you master Git and become a more effective coder! Let me know if you have any other favorite Git tricks.