git cheatsheet

git is a program for versioning and tracking changes for any kind of projects and often used for coordinated collaborative software development. git is truly one of those tools that makes the life of any programmer much easier.

This is my cheat sheet for my daily git life. But I want to first start with some useful links

Basics

branches

# Checkout remote branch
git checkout -b BRANCH origin/BRANCH

# List all existing branches
git branch -av

# Switch HEAD branch
git checkout BRANCH

# Create new branch based on your current HEAD
git branch NEW-BRANCH

# Delete a local branch
git checkout -d BRANCH

delete a remote branch

git push -d <remote_name> <branch_name>

Alternatively, this is the longer way:

git push <remote_name> --delete <branch_name>
# Note: <remote_name> is 'origin' in most cases

merge and rebase

# Merge BRANCH into your current HEAD
git merge BRANCH

# Rebase your current HEAD onto BRANCH
## (!!) Don't rebase published commits otherwise you will get (rightfully) yelled at!
git rebase BRANCH

# Abort a rebase
git rebase --abort

# Continue a rebase after solving conflicts
git rebase --continue

# Use configured mergetool to solve conflicts
git mergetool

# Use editor to manually resolve conflicts and (after resolving) mark files as resolved
git add ...
git rm ...

hard reset

git fetch; git reset --hard origin/`git rev-parse --abbrev-ref HEAD`

Rename master to main branch

$ git branch -m master main          # Rename locally
$ git push -u origin main            # Push main to remote
$ git push origin --delete master    # Delete master from remote

Depending on the remote, you might need to switch the default branch before step 3, otherwise it will be rejected.

What your teammates will have to do

git checkout master
git branch -m master main
git fetch
git branch --unset-upstream
git branch -u origin/main

Rename master to main locally, and then fetch the contents from upstream.

Config

useful aliases

Your ~/.gitconfig file supports aliases. Here are some of my often used ones for inspiration

[alias]
    c = checkout
    b = branch
    co = commit
    d = diff
    l = log --all --graph --oneline --decorate
    s = status --short --branch
    puff = pull --ff-only
    purr = pull --rebase
    ignore = update-index --assume-unchanged
    unignore = update-index --no-assume-unchanged

Thanks to w4rh4k for the puff and the purr aliases, which are awesome.

setting user and email

git config user.name phoenix
git config user.email user@email

Alternatively you can edit your ~/.gitconfig and/or .git/config

[user]
    name = phoenix
    email = user@email

Stash

A git stash is a temporary “snapshot” to save (or stash) your current changes, so you can work on something else and then later come back and re-apply your “snapshot”.

Safe current dirty workspace to a stash with

git stash       # Create stash
git stash list  # List stashes

Apply those changes (later) via

git stash apply
git stash apply stash@{0}
git stash pop      # Pops the last entry, i.e. apply and remove

Remove stashes

git stash drop              # Delete the last one
git stash drop stash@{0}

Creating a new branch from a stash

git stash branch BRANCH [<stash>]

Submodules

Git submodules are often used to incorporate another versioned project into an existing project

Adding a submodule is as easy as

git submodule add <remote_url> <destination_folder>
git commit -m "Added the submodule to the project."

When pulling a repository with submodules in it, you need to initialize them first

git submodule init
git submodule pull

update submodules

git submodule update --init --recursive

To pull the latest remote changes on git 1.8.2 and above, you can use the –remote

git submodule update --recursive --remote

Remove submodules

Manually edit the .gitmodules file.

git hooks

git hooks are basically bash scripts located in the .git/hooks directory that can be edited. Any git project should contain already some sample files therein.

For more information see also githooks.com.

adding hooks to the git version history

My way is to put the hooks to .githooks directory in the repository (it can be any directory) and then run

git config --local core.hooksPath .githooks/

Further topics

Licensed under CC BY-NC-SA 4.0
Last updated on Feb 02, 2023 21:34 +0200