Version control is at the heart of modern software development, and Git is the undisputed leader. While most developers are comfortable with basic Git commands like clone, add, commit, and push, mastering advanced Git and GitHub commands can dramatically improve your workflow.
In this guide, we’ll dive into some essential advanced Git and GitHub commands that every developer should know.
1. Clone a Specific Branch
Instead of cloning the entire repository:
git clone --branch branch_name --single-branch https://github.com/user/repo.gitOnly the specified branch is downloaded, saving time and space.
2. Stash Changes
Save your local modifications without committing them:
git stash3. Amend Last Commit
Made a small mistake in your last commit? Just amend it:
git commit --amendThis lets you modify the last commit message or add forgotten changes.
4. Squash Commits (Interactive Rebase)
To combine multiple commits into one (clean up your commit history):
git rebase -i HEAD~nReplace n with the number of commits you want to review.
Choose squash (or s) for commits you want to merge.
5. Force Push Changes
If you have rebased or amended commits:
git push --force** Caution:** Force pushing can overwrite history, so use it carefully, especially on shared branches!
6. Create and Track a New Branch
Create a branch and immediately switch to it:
git checkout -b new-branch-namePush and set upstream:
git push -u origin new-branch-name7. Fetch and Prune Deleted Remote Branches
Keep your local references clean:
git fetch -pRemoves references to remote branches that no longer exist.
8. View Detailed Commit History
For a cleaner log with graph-like visualization:
git log --oneline --graph --all --decorateGreat for visualizing branch merges and commit history.
9. Cherry-pick Specific Commits
Apply a single commit from another branch:
git cherry-pick commit_hashUseful when you need only one feature or fix without merging the entire branch.
10. Revert a Commit (Without Losing History)
If you need to undo a commit without deleting history:
git revert commit_hashIt creates a new commit that undoes the changes.
Mastering these advanced Git commands can take your productivity to the next level and help maintain a cleaner, more professional project history. Whether you're working on solo projects or collaborating in teams, becoming a Git power user is a career-defining skill.