Git by Task

This blog is a slightly different kind of a cheat sheet. It is based on common tasks that we do using git command line. Hopefully, this blog will reduce your memorization overload.

To Clone a Repository
git clone https://repositoryName.git

grab the repository.git part from the git web UI for cloning a repository. This command will create a directory with the same name as the last part right next to .git

To List All Available Branches
git branch -a
To See Which Branch You Are On and Its Status
git status
git branch
To Switch to a Branch
git checkout branchName
git pull
To Discard Changes You Have Made to a File Locally
git checkout fileName
To Commit File(s) to the Local Copy of the Repository
git commit -m "Useful commit message" file1 file2 file3
To Push Changes Made in a Branch to the Remote Repository
git push --set-upstream origin branchNameContainingChanges
To Create a New Branch and to Switch to It (local to your machine)
git branch newBranchName; git checkout newBranchName
git checkout -b newBranchName
To Delete a Local Branch
git checkout branchOtherThanOneYouAreDeleting
git branch -D branchYouWantToDelete
To List All Code Changes Between The Local Copy and Remote
git diff
To See the Full Path to the Remote Repository
git remote -v
To Show All Branches Graphically
git log --graph --decorate --oneline
To Merge the Remote Branch into Your Local Current Branch
git merge origin/branchName
To Download Source Code from a Specific Branch, but don’t Switch to It
git pull origin branchName
To Push Commits to the Remote Repository
git push
git push --set-upstream origin branchName
Using Rebase as a Strategy Instead of Merge
git checkout branchToRebaseTo
git pull
git checkout featureBranch
git pull
git rebase -i branchToRebaseTo
git pull
git push

then issue a pull request (PR) or merge request (MR) to merge the feature branch into the branchToRebaseTo

Restore a Deleted Folder

This is useful when you want to discard your changes in a certain folder and download that folder from the repo

git checkout -- path/to/folder




One thought on “Git by Task

  1. I would like to suggest a double message on the commit. `git commit -m “short what is this commit msg” -m “longer why did I do this msg”` Git and IDEs make use of the double message as a header/body of the full message. This can also be done by separating the header and body by 2 line feeds which is harder to do from the cmd line.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s