| 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 |
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)| 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 |
- Pull your favorite gitignore files from https://github.com/github/gitignore and concatenate them into a new file at ~/.gitignore
git config --global core.excludesfile ~/.gitignore- You now have a global gitignore file!
- 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.
git config --global credential.helper osxkeychain
- https://github.com/git-ecosystem/git-credential-manager
git config --global credential.interactive alwaysgit config --global credential.useHttpPath true- Issue with GCM? Try:
git-credential-manager-core diagnose
Options:
- GPG Suite (recommended by Github/easiest)
- Note: GnuPG is also recommended by GitHub but it doesn't have a way to access the OSX keychain
- Bug with devcontainers not importing GPG keys: microsoft/vscode-remote-release#9217
- pinentry-mac
- GitHub Doc: https://docs.github.com/en/authentication/managing-commit-signature-verification/generating-a-new-gpg-key
- More useful info: https://stackoverflow.com/questions/41502146/git-gpg-onto-mac-osx-error-gpg-failed-to-sign-the-data
git config --global commit.gpgsign=truegit config --global gpg.program=gpggit config --global user.signingkey=<gpg key>
git config --global user.name "first_name last_name"git config --global user.email "name@example.com"
Create a .gitattributes file and add the following line:
*.ipynb filter=strip-notebook-output`
gp - git push current branch to origin
alias gp='git push origin "$(git symbolic-ref --short HEAD)"'git ls-files | xargs wc -l | sortgit ls-files | egrep '.py$' | xargs wc -l | sortOr, to get the number of characters, use wc -c
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
}