Stefan Boos

Link collection and some blog posts

Follow me on GitHub

Miscellaneous Git Topics

Table of Contents generated with DocToc

Cheat Sheet

# Which files have changed after a special date?
# This is actually also the input format for code maat (https://github.com/adamtornhill/code-maat)
git log --pretty=format:'[%h] %an %ad %s' --date=short --numstat --after="2022-01-21 10:00"

# Show tags together with the first line of their annotation messages
git tag -l -n

Getting Started

As an introduction I recommend chapters 1 (Overview, History), 2 (Basics) and 3 (Branching, Merging) of the Pro Git Book.

Udacity has a free online course on git: Version Control with Git.

To deepen your knowledge, perform the exercises in GitHub: eficode-academy / git-katas.

Writing Commit Messages

Cleaning up (Remote) Branches

After merging, remote branches may still show up in git branch -a, in GitLens or in other visual git tools.

# Cleanup merged branches in the remote repository (origin)
git fetch --prune

The above command will git fetch --all && git remote prune. This will delete all merged and otherwise orphaned remote branches.

For a detailed explanation, see Atlassian BitBucket Tutorials: Git Prune.

Transfer Uncommited Changes Via Patch File

If you don’t want to use branches, merging and pull requests to bring changes over, then you can resort to patch files.

Quick Solution

Assume you are in a branch A of your repository on machine A

# Export all unstaged changes to a file "some.patch"
git diff > some.patch

Then on machine B, check out the same branch A and

# Apply the patch to the other repository
git apply some.patch

Special Cases

# Consider the staged files only
git diff --cached > some.patch

# Include binary files like .mp3 files
git diff --cached --binary > some.patch

# Consider both staged and unstaged files
git diff HEAD > some.patch

References Regarding Patch File Transfer

Merge With An Other Git Repository

Assume you have created a local git repository on your computer. Now you want to upload it to your favorite host. For this purpose you create a fresh repository at the remote.

Now you want to merge the new remote into your local repository and push everything back:

# The following command requires your local repository not to
# have an "origin" remote configured
git remote add origin https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git

# Force to merge the unrelated histories
# Caveat: Only do this if you know that the remote is really
#         fitting to your current branch!
git pull origin main --allow-unrelated-histories

# Rename "master" to "main", if applicable
git branch -m main

# Push your local changes to the remote
git push --set-upstream origin main
git push

References Regarding Merging With An Other Git Repository