https://git-scm.com/book/en/v2
┌───────── commit -a ───────────>│
├─── add -u ──────>│── commit ──>│
│ │ ├─── push ───>│
┌─────────┐ ┌─────┐ ┌─────┐ ┌──────┐
│workspace│ │index│ │local│ │remote│
└─────────┘ └─────┘ └─────┘ └──────┘
│<────────────── pull or rebase ───────────────┤
│ │ │<── fetch ───┘
│<────── checkout HEAD ──────────┤
│<─── checkout ────┤ │
│<────────── diff HEAD ─────────>│
│<───── diff ─────>│
git add
git branches
git fetch and pull
git reset and amend
git rebase
git stash
git diff
git tag
git ignore
git bisect
git worktree
git submodules
hosts
sourcehut
Very fast and simple email based git / project hosting site. Created by Drew DeVault.
https://codeberg.org/
A github style hosting site that doesn't suck. Occasional downtime though.
https://radicle.xyz/
An interesting peer to peer git hosting system.
https://tildegit.org/
Code hosting site used by lots of the smolweb community.
github
The big crappy code hosting site. Useful for finding new libraries.
misc
remove untracked files from the working tree
git clean -fd
Use -n
for a dry run and -x
to include "ignored" files/directories.
untrack a file
First add the file to your gitignore and commit the gitignore change
then run git rm --cached file
for each file.
cherry-pick from different repo
This is something I needed to do quite often for work. They have two separate repos. One is open source, the other is basically a closed source fork with some additional features. Pretty much every time you fix a bug you're expected to make a PR in both repos with the fix.
simple 1-off approach
In most cases the solution is really simple. Generate a patch file from the
source repo of the last commit or several commits. Then change to the other repo
and apply it with git am
:
git format-patch -1 HEAD
mv 0001-commit.patch ../other-repo/
cd ../other-repo
git am < 0001-commit.patch
For git am
there's the -3
option which attempt to do a 3-way merge if
applying the patch normally fails and also --ignore-whitespace
which ignores
changes in whitespace in context lines if necessary.
quick fetch
Another option is to quickly fetch the remote branch into your repo and
cherry-pick the specific commit hash you want:
git fetch <repo-url> <branch> && git cherry-pick <sha>
additional remote
The more complex solution, but sometimes nicer (the merging tools for git am
are a bit less powerful) is to add the other repo as a remote and then
cherry-pick the commit you want:
git remote add other https://example.link/repository.git
git fetch other
git cherry-pick <first_commit>..<last_commit>
# You could even do a git merge other/branch if you really need...
git remote remove other
clone in a script
If you're cloning say, thousands of repositories, you'll probably hit a few that have been deleted or made private and github or whatever will prompt for a password. This will halt your whole script which is very annoying. To instead fail and skip these simply set this variable:
export GIT_TERMINAL_PROMPT=0
https://commit.style/
A basic commit style "guide".