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 Discard Changes You Have Made to a Folder Locally, and Everything Inside that Folder
git checkout folderName
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 master
git pull
git checkout featureBranch
git pull
git rebase -i master
git pull
git push

then issue a pull request (PR) or merge request (MR) to merge the feature branch into the master. If/when conflicts are flagged and there may be some for each commit, resolve these one by one, followed by “git rebase –continue” command. For files under the section “Changes to be committed”, such as those flagged “modified” or “deleted” or “renamed”, nothing needs to be done.

For files under the section “Unmerged paths”, such as those flagged “deleted”, use the “git add fileName” to add the deletion (not intuitive). For files flagged as “both modified”, use “git checkout –theirs fileName” if you want the fileName from your branch instead of master, followed by “git add fileName”, and followed by “git rebase –continue” once all modified files have been added.

After each of the commits have been cleaned up, and git is happy. Use the “git push -f” to push the changes to the repo forcefully. Now, all of the commits are lined up in a row, starting at the end of the master branch.

Rebase method can be used in your pull request, just don’t do the merge step until after it has been reviewed. For further details see the explanation below.

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

git checkout master -- path/to/folder

Explanation of the Rebase Strategy

Rebase puts all of the commits from your branch lined up in a row, starting at the end of the branch you’re rebasing to, such as master.

The first two steps are to cleanse the git state. The next three steps are the core of the rebase method, which moves the entire feature branch to the tip of the master branch. The advantage over merge is cleaner git history, making it easier to understand.

A great resource is the following article, which compares rebase to merge, providing pro’s and con’s of each strategy:

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