Using Git
Clone one branch or tag, w/o history, with submodules w/o their history (shallow)
git clone -b v1.2.3 --recurse-submodules --shallow-submodules --depth 1 [repo]
Update a tag / release (rewrite an existing tag)
Use these Variables
tag: the GitHub tag name (e.g.v1.2.3)btag: temporary branch name created from the tagrtag: full tag ref path (refs/tags/<tag>), used for remote deletion
Example: tag=v1.2.3
btag="b$tag"
rtag="refs/tags/$tag"
1) Create and switch to a temporary branch from the existing tag
git fetch origin --tags
git switch -c "$btag" "$tag"
2) Make changes, commit, and push the temporary branch
git commit -am "Fixes for $tag"
git push -u origin "$btag"
3) Delete old local tag pointing to the old commit, recreate the tag locally at the new HEAD
(You’re already on $btag, so the extra git switch "$btag" is redundant.)
git tag -d "$tag"
git tag "$tag"
4) Replace the remote tag pointing to the new commit
First delete the old remote tag, then push the new one. Force-update the remote tag explicitly because tags are immutable by convention and many servers reject non-fast-forward tag updates unless forced.
git push origin ":$rtag"
git push --force origin "$rtag"
5) Update the GitHub Release (and assets) if necessary
- If you uploaded release assets (binaries / custom .tar.gz / etc.): update the existing release by removing the old assets and uploading the new ones (or delete and recreate the release). Moving the tag does not replace uploaded assets.
- If only GitHub-generated source archives are used (“Source code (tar.gz/zip)”): you can usually leave the release as-is; it will continue to be “for $tag”, and the generated archives will follow the updated tag (may take a short while due to caching).
- If release notes/changelog should change: either edit the existing release notes, or delete and recreate the release for $tag.
6) Cleanup: delete the temporary branch (remote + local)
git push origin --delete "$btag"
git switch main
git branch -D "$btag"
Branches
Create new branch and switch to it:
git checkout -b branchname
Switch to an existing branch:
git checkout branchname
Merge changes from branch1 into branch2 (one of them can be main). Use rebase for a clean history.
Alternatively, you can use merge to keep the history of both branches.
git checkout branch2
git merge/rebase branch1
Push a branch to remote:
git push -u origin branchname
Delete a remote branch:
git push origin --delete branchname
List all remote branches:
git branch -r: lists all remote branchesgit branch -a: lists all branches (local and remote)git branch -vv: lists all branches with their last commitgit branch --merged: lists branches that have been merged into the current branchgit branch --no-merged: lists branches that have not been merged into the current branch
Conflicts
1. Resolve Conflicts
- Check what’s conflicted:
git status
- Text conflicts (
UU, etc.): open file and fix<<<<<<< ======= >>>>>>>, then:
git add path
- Keep one side (choose ours or theirs):
git checkout --ours path # keep our version (rebase source)
git checkout --theirs path # keep their version (rebase target)
git add path
- You want the file removed (e.g.
DU/ delete-vs-modify conflict):
git rm -- path
# if the file is already gone locally:
git rm --cached -- path
git add -u
2. Finalize a rebase or a merge
git rebase --continue # repeat until it finishes
git push origin branch2
or
git commit -m "merge branch1 into branch2"
git push origin branch2
Unmerged status codes
DDboth deletedAUadded by usUDdeleted by themUAadded by themDUdeleted by us (often: decide keep vs delete; usegit rmto delete)AAboth addedUUboth modified
History
Show commit + diff for a specific file
git log -p -- path/to/file
Compact one-line history
git log --follow --date=short --pretty=format:"%h %ad %an %s" -- path/to/file
To remove all history from a repo:
git checkout --orphan new-main
git add -A
git commit -m 'new files'
git branch -D main
git branch -m main
git push -f origin main
git branch --set-upstream-to=origin/main main
Tokens
Personal Settings > Developer Settings
Can regenerate with custom expiration date.
Configuration:
repo –> public_repo
For private images, the token must have at least read:packages.
The token should go in a file read by the software using it, typically in the home dir, for
example .bob