Skip to content

Instantly share code, notes, and snippets.

@benc-uk
Last active March 22, 2024 23:14
Show Gist options
  • Save benc-uk/5df97c0d572169eacf25f89272ed8ac6 to your computer and use it in GitHub Desktop.
Save benc-uk/5df97c0d572169eacf25f89272ed8ac6 to your computer and use it in GitHub Desktop.
Git Cheatsheet & Recipes

Git

Cheatsheets and common recipes for git

Recipe - Ammending commits

Use when doing tedious work requiring many similar trivial commits & pushes, e.g. CI/CD YAML pipelines etc

git commit --amend --no-edit
git push -f

Or as one liner

git add -A && git commit --amend --no-edit && git push -f

The Basics

Command Description
git init Create a git repo in current folder
git clone https://blah/repo Clone a remote repo
git remote add origin https://github.com/user/blah.git
git push -u origin master
echo "# foo" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/user/blah.git
git push -u origin master

Remotes

Command Description
git push Default push (normally to upstream, see push.default docs)
git push -u origin mybranch Push branch mybranch to origin and set it to be upstream
git pull Pull changes from default remote and bring everything up to date
git pull someremote mybranch Pull branch mybranch from remote someremote
get fetch Download remote changes but don't merge them, they will be in FETCH_HEAD
git fetch --prune Re-sync local and remote branches

Branches

Command Description
git branch mybranch Create branch but don't change to it
git checkout -b mybranch Create branch and change to it
git checkout mybranch Change to existing branch
git branch -d mybranch Delete local branch
git branch -D mybranch Delete local branch force
git push -d origin mybranch Delete local & remote branch

Using multiple SSH keys with GitHub

Create a SSH new key

cd ~/.ssh
ssh-keygen -t rsa -C "user_blah"

Save to a new file, for this example I'll use the name new-git-key but it can be anything of course!

Enable SSH agent

Add the SSH agent to your bash/zsh start up script(s), i.e. .bashrc or .zshrc

This does:

  • Enables and runs the agent in the background
  • Adds your keys to the agent, place these lines in the same start-up script (they don't persist otherwise) just after enabling it. Add your default key (normally called id_rsa) and your new one.
eval $(ssh-agent -s)
ssh-add ~/.ssh/id_rsa 2> /dev/null
ssh-add ~/.ssh/new-git-key 2> /dev/null

Configure SSH per host

Create ~/.ssh/config if it doesn't exist, and add the following.
The name github_new is effectively an alias, and you can pick any name you like.

Host github_new
 HostName github.com
 IdentityFile ~/.ssh/new-git-key

Use in Git

Then use the alias github_new instead of hostname github.com in your git operations

Example:

git clone git@github_alt:some-org/some-repo.git

Recipe - Patch update an existing tag

  • Create a branch from the tag

    • git branch {tagname}-branch {tagname}
    • git checkout {tagname}-branch
  • Include the fix manually if it's just a small change

    • git add .
    • git commit -m "Fix included"
      or cherry-pick the commit
    • git cherry-pick {num_commit}
      or merge a branch (e.g. master) with fixes/patch
    • git merge master
  • Delete and recreate the tag locally

    • git tag -d {tagname}
    • git tag {tagname}
  • Delete and recreate the tag remotely

    • git push --delete origin {tagname}
    • git push origin {tagname}
  • Update local repository with the updated tag

    • git fetch --tags
  • Merge back to master if required

    • git checkout master
    • git merge {tagname}-branch
    • git branch -d {tagname}-branch

Tags

Create a tag - git tag <tag_name>
Push a tag - git push origin <tag_name>
Delete local tag - git tag -d <tag_name>
Delete remote tag - git push --delete origin <tag_name> Delete remote tag - git push origin :refs/tags/<tag_name>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment