2 Git
Tony Mottaz edited this page 2026-01-17 03:44:16 +00:00

Rebasing

Rebase feat onto main, preferring the contents of main when encountering conflicts:

git switch feat
git rebase main -X ours

The ours in this case refers to the so-far rebased series, starting with main, and theirs is the working branch.

Worktrees

Set up a temporary workspace, like a second clone of the repo. This is useful if you need to quickly switch into a branch while in the middle of working on another branch.

# this will also create a new branch with
# the name of the final component of <path>
# git worktree add <path>
git worktree add ../hotfix-123

# ALTERNATIVE
# create the worktree from an existing branch
# git worktree add <path> <branch>
git worktree add ../hotfix-123 main

# list worktrees
git worktree list

# remove the worktree
# git worktree remove [-f] <worktree>
git worktree remove ../hotfix-123

Remotes

# list remotes and their URLs
git remote -v

Aliasing

git config --global alias.whatever 'shell script'

# Examples
git config --global alias.co checkout
git config --global alias.unstage 'reset HEAD --'

By default, aliases are interpreted as subcommands of git. To run an external command instead, prefix with !

git config --global alias.visual '!gitk'

Automatic config based on filepath

Reference: How I configure my Git identities

Automatically using different configuration files based upon the parent tree structure:

[includeIf "gitdir:~/idophp/"]
path = ~/idophp/idophp.gitconfig

[includeIf "gitdir:~/siftware/"]
path = ~/siftware/siftware.gitconfig

Create an independent version of checked-in file

This will let you save local changes to a tracked file without creating a diff in Git.

git update-index --skip-worktree <path>

To undo this, use the no- variant:

git update-index --no-skip-worktree <path>

Undoing things

Undo a commit and keep the changes

git reset HEAD~1 --soft

Create a repo archive

Use git-archive. For example:

git archive --format=zip -o my-repo.git.zip HEAD

Local-only "ignore" file

The .git/info/exclude file in the repository is where you can specify ignore patterns that only apply to you.

echo "page/tony-demo/" >> .git/info/exclude

h/t Josh Farrant