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.

To Cloning a Repository

git clone https://something.git

grab the something.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

To Discard Changes You Have Made to a File

git checkout fileName

To Commit File(s) to the Local Copy of the Repository

git commit -m "Useful commit message" file1 file2 file3

To Create a New Branch and to Switch to It (local to your machine)

git branch newBranchName; git checkout newBranchName
or
git checkout -b newBranchName

To Push the New Branch to the Remote Repository

git push --set-upstream origin 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

 

 

 

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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