Another Git Cheat Sheet

Basic work flow

*Note: square brackets ("[ ]") indicate sections of a command that should be replace with user specific input.

Clone

First in your shell navigate to the directory where you want to clone the repository, then run:

git clone [repository url] [repository name]

Checkout a branch

While in directory where your local repository lives:

git checkout [branchName]

If you want to check out a brach from a remote there are two options, in detached head state, or create the branch locally

Detached head:

git checkout [remoteName]/[branchName]

Local branch:

git fetch [remoteName]
git checkout [branchName]

Make a commit

Check the status of your files:

git status

Add files to your commit:

git add [path/fileName]

Or add all:

git add -A

Or interactivally select what to add:

git add -p

Commit the code:

git commit -m "[commit message goes here]"

Push your changes to the remote branch:

git push

Rebase a branch with master/trunk/main

In these instructions are written in the context of having two different remotes, "upstream" being the main repository for an app and "origin" being a fork of that app.

Make sure you are on your local master/trunk/main branch then fetch the remote you are updating from:

git fetch upstream

Now we are going to rebase the branch we are on with our local master/trunk/main branch:

git rebase upstream/master

Then update the origin master/trunk/main branch:

git push origin/master

In some cases if the branches have drifted or the history was rewritten you might need to force push:

git push origin/master --force-with-lease

Add a remote

Get the repo url for the new remote from github

git remote add [remoteName] [remoteURL]

Then we will want to check and make sure remote was successfully added

git remote -v

Rename a remote

After a remote has been added:

git remote rename [currentRemoteName] [newRemoteName]

Set a branch remote

Create your branch locally then:

git push --set-upstream [remoteName] [branchName]

Helpful bits

See your changes

See the difference in file changes you have made so far. From your working directory:

git diff

If you would like to exclude white space changes:

git diff -w