DEV Community

Jegadeep Pandiarajan
Jegadeep Pandiarajan

Posted on

Advanced Git and GitHub Commands

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.git
Enter fullscreen mode Exit fullscreen mode

Only the specified branch is downloaded, saving time and space.

2. Stash Changes

Save your local modifications without committing them:

git stash

Enter fullscreen mode Exit fullscreen mode

3. Amend Last Commit

Made a small mistake in your last commit? Just amend it:

git commit --amend

Enter fullscreen mode Exit fullscreen mode

This 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~n
Enter fullscreen mode Exit fullscreen mode

Replace 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
Enter fullscreen mode Exit fullscreen mode

** 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-name
Enter fullscreen mode Exit fullscreen mode

Push and set upstream:

git push -u origin new-branch-name

Enter fullscreen mode Exit fullscreen mode

7. Fetch and Prune Deleted Remote Branches

Keep your local references clean:

git fetch -p
Enter fullscreen mode Exit fullscreen mode

Removes 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 --decorate

Enter fullscreen mode Exit fullscreen mode

Great for visualizing branch merges and commit history.

9. Cherry-pick Specific Commits

Apply a single commit from another branch:

git cherry-pick commit_hash
Enter fullscreen mode Exit fullscreen mode

Useful 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_hash
Enter fullscreen mode Exit fullscreen mode

It 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.

Top comments (0)