Git Cheat Sheet

January 15, 2022
 
Common Git operations
I flip-flop between multiple development languages in multiple environments, so cheat sheets help.
 

Pulling a repository locally for the first time

To get the code from an existing git repository downloaded to your local machine for the first time:
git init
git remote add -f origin
https://MyUsername@bitbucket.org/MyRepo/MyProject.git
git pull origin master
 

Committing your updates

After you've updated your code locally, how to push the changes back up to the repository:
git add -A
git commit -m "Initial Commit"
git push
 

Tagging

Create Remote Tag

To capture a snapshot of your code at a specific point you could tag it:
git tag -a v0.0.1 -m "Version 0.0.1 Before Release"
git push origin v0.0.1
git checkout v0.0.1
 

Delete Remote Tag

git push --delete origin tagname

Revert

UNDO local file changes and KEEP your last commit

git reset --hard

Merging

To merge branches into master:
git checkout master
git merge otherBranch
Delete old local and remote branch
git -d otherBranch
git push origin -d otherBranch
 

Sparse Checkout

If you're setting up continuous integration for automated builds and deployment you may only want to get specific directories from your repository. To do this you could use the sparse feature of git as follows:
git init
git config core.sparsecheckout true
echo source/MyProject | out-file -encoding ascii .git/info/sparse-checkout
git remote add -f origin https://MyUsername@bitbucket.org/MyRepo/MyProject.git
git pull origin master
 


Common Issues and Solutions


Mac Command Line Tools

Sometimes after you do a macOS update you will see this error when attempting to use git:
xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
This occurs because the Xcode Command Line tools need to be updated. You can do this as follows:
xcode-select --install
After installation your git should be operational again.
 

 
Return to articles