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

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

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:


Conflicts

1. Resolve Conflicts

git status
git add path
git checkout --ours  path   # keep our version (rebase source)
git checkout --theirs path  # keep their version (rebase target)
git add path
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


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