
1. Convert a repository into a Git repository
This will create a .git sub folder in your project directory.
$ cd <PROJECT_DIRECTORY>
$ git init
2. Clone a repo and pull the latest changes
$ git clone <GIT URL>
$ git pull
3. Create a new branch
$ git checkout -b <DESCRIPTIVE BRANCH NAME>
# View all branches
$ git branch
# Checkout an existing branch
$ git checkout <BRANCH_NAME>
4. Delete a branch
# Delete a local branch
$ git branch -d <BRANCH NAME>
# Delete a remote branch
$ git push origin --delete <BRANCH NAME TO DELETE>
5. Rename a branch
# From the branch to be renamed
$ git branch -m <NEW BRANCH NAME>
# From another branch
$ git branch -m <OLD BRANCH NAME> <NEW BRANCH NAME>
# Steps to rename a remote branch after following the above steps
$ git push origin -u <NEW BRANCH NAME>
# Delete old remote branch
$ git push origin --delete <OLD BRANCH NAME>
6. Pull the latest changes from the master branch
There are two ways to pull the latest changes from master into your branch. Tip: If you are working on a complex change, pull the changes from master often (once or twice everyday depending on how frequently new changes are shipped to master). This will save a lot of time not having to deal with merge conflicts.
Do a merge
Note: In this way, you can checkout master and pull all the latest changes into your branch but will have to solve multiple conflicts that arise from this scenario. Solve any conflicts that come up after executing the following commands.
$ git checkout <YOUR BRANCH>
$ git merge master
Do a rebase
$ git checkout <YOUR BRANCH>
$ git rebase master
Not sure whether to do a merge or rebase? Checkout this tutorial on merging vs rebasing.
7. Commit and push changes
# Check the changes you have made
$ git diff
# Stage all the changes
$ git add .
# Stage only a single file
$ git add <FILE NAME>
# Commit the changes
$ git commit -m "Commit message"
# Stage changes and commit one-liner
$ git commit -am "Commit message"
# Check if everything looks okay
$ git status
# Push changes to remote
$ git push origin master
8. Save changes locally without committing
# Save uncommitted changes in a stack where you can get it back later
$ git stash
# To retrieve the stash and apply it on top of your branch
$ git stash apply
# Save stash with a name
$ git stash push -m "Name of the stash"
# To view list of all stashes
$ git stash list
# Apply a stash with index n
$ git stash apply stash@{n}
# Apply a stash and pop it from stack
$ git stash pop stash@{n}
9. Pull the latest changes into a branch
This can result in change conflicts which have to be resolved.
$ git pull
10. Check commit history
This will display an entire scrollable commit history of your repository.
$ git log
11. Merge branch with master
# Squash and merge if there are too many noisy commits
$ git checkout master
$ git merge --squash <BRANCH TO MERGE>
$ git commit
# Regular merge
$ git checkout master
$ git merge <BRANCH TO MERGE>
$ git push origin master
12. Something’s on fire! Revert!
# Revert a single commit
$ git revert <COMMIT SHA>
# Revert Multiple commits
# Note: Works well only if there are no merge commits done
$ git revert <OLDEST COMMIT SHA>..<LATEST COMMIT SHA>
# Revert multiple commits and retain commit history
# Note: Works with merge commits
$ git checkout -f <TARGET COMMIT SHA> -- .
$ git commit -m 'revert to <TARGET COMMIT SHA>'
$ git diff HEAD # To check
These commands have helped me get 90% of my work done during all these years. Special scenarios have been pretty rare and they definitely warrant a deeper understanding before diving into them. Good luck!

One thought on “Essential Git Commands To Improve Project Workflow”