Skip to content

masonsmorales/git-cheat-sheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 

Repository files navigation

Git Commands Cheat Sheet

Command Notes
git switch some_branch If some_branch exists on remote but not local, it will pull it from the local repo and switch to it
git reset HEAD~1 Undo the previous commit but not the changes
git reset --hard origin/some_branch Destructive command to remove any uncommitted changes
git branch -m new_branch Rename current branch to new_branch
git diff > some_patch Save a working copy of your changes to a file called some_patch
git apply some_patch Applies the some_patch patch file to the current branch
git format-patch <commit_hash> Creates a patch file with the changes from that commit
git clone --single-branch --branch=master --bare clone_url Clones down a .git/ folder containing only the master branch
git clone url Clones a repo
git checkout -b branch_name Creates a new branch and switches to it
git status List changed files
git add --all Add all files that are not in .gitignore
git commit -m "commit message" Create a new commit
git log Log file of all commits
git reset --soft HEAD~3 Remove the last 3 commits on the branch (does not lose changes, userful for squashing locally)
git push -f origin my_branch Force push my_branch (overwrites remote branch; useful after a local squash when remote was pushed but not squashed; resolves "Updates were rejected because the tip of your current branch is behind its remote counterpart")
git reset --hard origin/master && git pull -v --rebase origin master && git checkout master && git pull Discard all local changes and re-pull the master branch; useful in a gitfix bash alias
git add -u Only adds currently tracked files (which have been modified) to the staging area and also checks if they have been deleted (if yes, they are removed from staging area). Does not stage new files
git fetch origin && git rebase origin/main Rebases a feature branch onto the latest version of the main branch
git restore <file> [options] Unstage and/or discard local changes to a file
git checkout HEAD -- <file> Perform a hard reset of changes made to file back to HEAD
git config --list --show-origin Show all global git configurations and the file path of each config setting
git branch -D some_branch Delete some_branch locally
git pull origin main --no-ff Merge changes from the main branch into your current branch

Backups

Create a backup copy of current branch

git branch $(git rev-parse --abbrev-ref HEAD)--$(date "+%Y%m%d_%H%M") && git branch -a | grep -i $(git rev-parse --abbrev-ref HEAD)

Git Stash

Command Notes
git stash push -m "my_stash_name" Stash changes by name
git stash Stash current uncommitted changes
git stash list List stashes
git stash pop Apply your last stash
git stash pop stash@{<n>} Pop the nth stash
git stash apply <i> Apply the stash with index i
git stash drop stash@{<n>} Delete/drop the nth stash
git stash branch <branch_name> [<stash_index>] Creates a new branch from a stash and then drops the stash; if stash_index is not specified, uses the latest one

Useful Git Configurations

Global gitignore file

  1. Pull your favorite gitignore files from https://github.com/github/gitignore and concatenate them into a new file at ~/.gitignore
  2. git config --global core.excludesfile ~/.gitignore
  3. You now have a global gitignore file!
  4. Note: You will probably still want to add a .gitignore file to the repos you are working on if multiple people are working on the repository.

Use OSX Keychain for HTTPS Repo Auth

  • git config --global credential.helper osxkeychain

Reuse Tokens from Web Auth for Git CLI Auth using GCM

GPG Key Setup (OSX)

Options:

Configure Username and Email

  • git config --global user.name "first_name last_name"
  • git config --global user.email "name@example.com"

Strip Python notebook output from git

Create a .gitattributes file and add the following line:

*.ipynb filter=strip-notebook-output`

Useful Bash Aliases

gp - git push current branch to origin

alias gp='git push origin "$(git symbolic-ref --short HEAD)"'

Other Tips & Tricks

Count the number of lines of code in a git repo

git ls-files | xargs wc -l | sort

Count the number of lines of Python code in a git repo

git ls-files | egrep '.py$' | xargs wc -l | sort

Or, to get the number of characters, use wc -c

Create a function to find all instances of a string in git-tracked files

In ~/.bashrc

# Create a function called "ggrep" (git grep)
ggrep() {
  if [ -z "$1" ]; then
    echo "Usage: ggrep <pattern>"
    echo 'Note: pattern supports Extended Regular Expressions (ERE)'
    echo 'To add egrep args, such as "-i" simply pass them with your pattern in double quotes,'
    echo '  e.g. ggrep "-i mypattern" or ggrep "mypattern -i" (order does not matter)'
  else
    git ls-files | xargs egrep -n --color=auto $1
  fi
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •