10 Common Git Commands

Here are some common git commands that I use on a regular basis to create pull requests on GitHub –

1. Shows all remotes
git remote

2. Shows remotes with their url
git remote -v

3. Gets remote data into local repo but doesn’t add to your working files
git fetch <remote>

4. Merge fetched data
git merge <remote>/master

5. Fetch and merge remote data with our working files
git pull
OR
git fetch <remote>
git checkout master
git merge <remote>/master

6. Add a remote
git add remote <name> <url>

7. Add remote branch as a local branch
git checkout --track <remote>/<branch_name>

8. Delete a branch locally
git branch -d <branch_name>
git branch -D <branch_name> force delete

9. Delete remote branch
git push <remote_name> --delete <branch_name>

10. Create and checkout a branch
git checkout -b <branch_name>

Bonus

I know I said 10 in the title but here are some more –

11. Rebase current branch with another branch.
git rebase <branch_name>

This will “port” another branch (Branch A) into the branch where you are currently working (Branch B), by applying all of your unique commits from Branch B on top of Branch A and replacing Branch B with this revised version. That way, you can “catch up” to other branches by re-writing git history in your local git. [Source]

12. See commits in current branch that aren’t cherry picked to master
git cherry -v master

13. Push a branch to remote
git push <remote> <branch>
git push <remote> <branch> -f force push

That’s all for now. If you’d like, go ahead and bookmark this page as I may add more to it in the future.

Leave a comment