Git Cheat Sheet

March 17, 2021

I’ve dipped my toes in the Git waters for over 10 years. I must confess that I never really grokked it. It might be that I never worked in a multi-user repo environment with Git. Or it might be that working with it for one month every year wasn’t enough to make it stick. So I decided to make my life easier with a Cheat Sheet(s).

View Branches

Use these command variants to get branch information.

Local branchesgit branch
Remote branchesgit branch -r
All branchesgit branch -a

For full information regarding your branch origin, fetch/push URLs, and other information, try this.

PS> git remote show origin

View Remote Repositories

View all remote repositories and their full URLs for fetching and pushing.

Just get repo namesgit remote
Get repo names and URLsgit remote -v

Save your Work before a Major Change

Ever want to just save your work off before you do something that can really screw up your work? Try this but remember to clean up later.

PS> git commit -a -m "Saving my work, just in case"
PS> git branch my-saved-work

Clean up later with a simple branch delete.

# Delete branch after it has been MERGED TO REMOTE REPO.
PS> git branch -d my-saved-work

# Delete branch if it HAS NOT BEEN MERGED TO REMOTE REPO.
#    (We can use "--delete --force" or shortcut "-D"
PS> git branch -D my-saved-work

# Delete branch from remote repo.
PS> push origin -d my-saved-work

Delete Untracked Files from Working Tree

Git clean can be a very handy command to delete untracked files. But it also is dangerous so be sure and use the -n (dry run) option first.

git clean [-d] [-f] [-i] [-n] [-q] [-e <pattern>] [-x | -X] [--] <path>…​

Dry rungit clean -n
Interactive modegit clean -i
Remove untracked files and foldersgit clean -fd

Move File to another Folder

The process is to create the new folder if necessary then execute the git mv filename new_folder command.

PS> git mv  .\DevBuild.targets  ..\..\MSBuild\Targets
PS> git commit -m "Moved file to Targets"

Reset Local Branch to Remote Repo HEAD

Depending on your exact goal, this can have multiple steps. This is the approach where you want to use if you just want to set your branch to the point it was when you branched it last. If in doubt, you might want to save your work first.

PS> git fetch origin
PS> git reset --hard origin/master

Git Fetch

Downloads the remote changes but does NOT update your local working folders. This is safer than git pull.

Git Pull

Very related to git fetch but as a second step, it will perform a git merge to create a merge commit. This could result in conflicts and starts the merge conflict resolution flow.

Git Push

This is the command to upload local repo content to the remote repo.

Git Sync

This function is not a true git command. Instead it is a shortcut used in Visual Studio (and other apps) to make the two step process of updating your local files and pushing your local changes to the remote repo. It performs the following commands: get pull then get push.

Git Add and Git Reset

To add files to the staging area, use the Add command. You can specify single files or multiple files.

PS> git add <file>
PS> git add *.cs

To remove a file that you do not want to be part of the staged files, use the Reset command.

PS> git reset <file>
PS> git reset *.cs

To remove all files from the staging area, use the Reset command with no parameters.

PS> git reset

Rename Git Branch

1. Checkout local branch you want to rename.

PS> git checkout <old_name>

2. Rename the local branch.

PS> git branch -m <new_name>

3. If the old name exists in the remote repository because you have already push content, rename the branch to the new name.

PS> git push origin -u <new_name>

4. Again – if the old name exists in the remote repository because you have already push content, delete the old name.

PS> git push origin --delete <old_name>

Delete Local Branches

PS> git branch -d <local_branch>

If Git refuses to delete the branch, it might be because there are unmerged commits in the branch. To force a delete, use the capital D – and be careful this is really what you want to do.

PS> git branch -D <local_branch>

Delete Remote Branches

PS> git push origin --delete <remote_branch_name>