Skip to content

Instantly share code, notes, and snippets.

@wdhowe
Last active February 3, 2024 21:01
Show Gist options
  • Save wdhowe/9ff285d56b62d68457364df0d1d207b2 to your computer and use it in GitHub Desktop.
Save wdhowe/9ff285d56b62d68457364df0d1d207b2 to your computer and use it in GitHub Desktop.
Git Commands Cheat Sheet

Git Commands Cheat Sheet

Configuration

List Current Global Settings

git config --global --list

Global Gitignore

Use for project agnostic/global gitignore files specific to a developer's setup.

Example contents:

.vscode/
touch ~/.gitignore
git config --global core.excludesfile ~/.gitignore

Create the remote upstream branch if it does not exist

git config --global --add --bool push.autoSetupRemote true

Working with Files

Checkout a file from another branch

git checkout OTHERBRANCH -- path/to/file

Permissions

Add executable permissions to a file and track in git.

git update-index --chmod=+x script.sh

Submodules

Restore submodule state

To restore a submodule to its original committed state:

git submodule update --init

Change a submodule to a specific branch

# Update submodule
cd [submodule directory]
git checkout OTHERBRANCH
git pull

# Commit submodule hash change to main project
cd ..
git add [submodule directory]
git commit

Rebase

Rebase a branch on main

git pull origin main --rebase
git push --force

Rebase main fork on upstream/main

  • From the forked project, add the upstream project
git remote add upstream git@github.com:UPSTREAM-PROJECT.git
  • Retrieve upstream changes to main
git fetch upstream
git rebase upstream/main
  • Push changes to your forked main
git push
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment