git
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| git [2026/06/06 22:17] – [Diff] jhagstrand | git [2026/06/14 09:18] (current) – [Squash Merge] jhagstrand | ||
|---|---|---|---|
| Line 29: | Line 29: | ||
| * HEAD - ? | * HEAD - ? | ||
| * detached head - ? | * detached head - ? | ||
| + | * tree-ish ? | ||
| * commit-ish | * commit-ish | ||
| * SHA-1 value | * SHA-1 value | ||
| Line 37: | Line 38: | ||
| * fourth: tags | * fourth: tags | ||
| * refs: branch, tag, remote | * refs: branch, tag, remote | ||
| + | |||
| + | Commands and things: | ||
| + | * Execute the branch command to create a branch. | ||
| + | * Execute the remote command to create a remote. | ||
| + | * Execute the commit command to create a commit. | ||
| + | * Execute the diff command to create a diff. | ||
| + | * Execute the tag command to create a tag. | ||
| + | * Exceptions: | ||
| + | * log, checkout, merge, push, pull are just commands. | ||
| + | * repository and worktree are just things. | ||
| ====repository==== | ====repository==== | ||
| Line 78: | Line 89: | ||
| ====commit==== | ====commit==== | ||
| - | The "commit" is the unit of version control. | + | |
| - | Each commit is a version of the source code. | + | The commit |
| + | |||
| + | Each commit is a new version of the source code. | ||
| + | |||
| + | Initial edits to a text file are tentative. | ||
| Each commit is given a unique 20-character name by sha1. | Each commit is given a unique 20-character name by sha1. | ||
| - | To display the history | + | The chain of commits |
| - | $ git log | + | $ git log # display a list of all the commits in reverse chronological order |
| - | $ git log --pretty=oneline | + | $ git log --oneline |
| + | $ git diff # compare the changes made between two commits | ||
| + | The programmer composes a commit message for each commit. | ||
| + | |||
| + | $ git commit -m ' | ||
| + | $ git commit | ||
| + | |||
| + | The multi-line commit message. | ||
| + | line 1 is title, max 50 chars | ||
| + | line 2 is blank, this is what signals git to split title and body | ||
| + | line 3+ is body, max 72 chars per line, hit enter to hardcode each newline | ||
| + | |||
| + | The commit message title style guide. | ||
| + | * Like a document sub-heading | ||
| + | * Imperative | ||
| + | * Start with capital letter, no period at the end | ||
| + | * No articles or filler | ||
| + | |||
| + | |||
| + | The history log of commit messages, combined with skillful use of the branch, merge, reset, and rebase commands enable a programmer to communicate to future developers exactly what he changed and how, why and when. | ||
| ====tag==== | ====tag==== | ||
| Line 102: | Line 136: | ||
| git show # details of latest commit | git show # details of latest commit | ||
| git tag -d 1.0 # delete a tag | git tag -d 1.0 # delete a tag | ||
| + | |||
| + | Tags are often used to implement public release versioning. | ||
| + | |||
| + | * Marketing Versioning, like macOS Sonoma | ||
| + | |||
| + | * Calendar Versioning, like 2026.06.04 | ||
| + | |||
| + | * Semantic Versioning (SemVer) | ||
| + | * see https:// | ||
| + | * major.minor.patch, | ||
| + | * minor and patch releases maintain backward compatibility | ||
| + | * as soon as backward compatibility is broken, the major number must be incremented | ||
| + | * important for a library with a public api, for example | ||
| + | |||
| + | (I remember the good old days, when broken backward compatibility was a sign of incompetence.) | ||
| + | |||
| ====worktree==== | ====worktree==== | ||
| Line 135: | Line 185: | ||
| in the the meantime, to bring master changes into the feature, do a rebase, | in the the meantime, to bring master changes into the feature, do a rebase, | ||
| - | handle the conflicts now, so the future feature merge will run without conflicts | + | * handle the conflicts now, so the future feature merge will run without conflicts |
| + | * handle conflicts conflicts in the feature branch, not when attempting the final feature merge | ||
| - | handle | + | |
| + | ==== Merge ==== | ||
| + | |||
| + | # merge a feature branch back into the master branch | ||
| + | git checkout master | ||
| + | git merge feature | ||
| + | |||
| + | That simple command "git merge feature" | ||
| + | |||
| + | First it checks whether any changes have been made to the master branch. And if so, it checks whether there are conflicts | ||
| + | |||
| + | - No changes made on master branch. | ||
| + | |||
| + | - Changes have been made on the master, but no conflicts. Now we are in a merge situation. Instead of doing a fast forward, it merges the updated files as necessary and then automatically commits those updated files. | ||
| + | |||
| + | - Changes have been made on the master, and there are conflicts. | ||
| + | |||
| + | |||
| + | Starting situation\\ | ||
| + | There are commits on the feature branch.\\ | ||
| + | What about the master branch? | ||
| + | * no commits on the master | ||
| + | * commits on the master but no conflicts | ||
| + | * commits on the master with conflicts | ||
| + | |||
| + | Desired outcome.\\ | ||
| + | The master branch is up-to-date, and the feature branch is gone.\\ | ||
| + | But how will the log look? There are three possibilities: | ||
| + | * Linear. The feature branch commits are all present in the master branch, as if the branch never existed. | ||
| + | * Indented. The feature branch commits have are all present, but indented under a merge commit, so you can see there must have been a branch there at one time. | ||
| + | * Collapsed. The feature branch commits are all gone, replaced by a single merge commit with a hand-written merge message that summarizes all the commits of the branch. | ||
| + | |||
| + | ==== Squash Merge ==== | ||
| + | |||
| + | The --squash option, the merge command does not really do a merge. | ||
| + | |||
| + | This is one of the ways to collapse a developer' | ||
| + | |||
| + | # Make sure you're on the branch you want to merge into | ||
| + | git checkout master | ||
| + | |||
| + | # merge the feature branch with --squash option | ||
| + | # this stages all the file changes into the master branch | ||
| + | git merge --squash feature | ||
| + | |||
| + | # ask for status now and you'll see all the changes ready for commit | ||
| + | git status | ||
| + | |||
| + | # now commit | ||
| + | git commit -m "feat: add feature command" | ||
| + | |||
| + | # delete the feature branch | ||
| + | git branch -D feature | ||
| + | |||
| + | # for fun do a log all between these commands to see what's going on | ||
| + | git log --all --oneline -15 --graph --decorate | ||
| + | |||
| + | ==== Log ==== | ||
| + | |||
| + | git commit adds an entry to the log. | ||
| + | |||
| + | How to fuck with the log. | ||
| + | |||
| + | * git merge | ||
| + | * git rebase | ||
| + | * git reset | ||
| + | |||
| + | git reset --hard HEAD^ | ||
| + | Effectively deletes the most recent commit. | ||
| Line 144: | Line 263: | ||
| The checkout command has two different functions. | The checkout command has two different functions. | ||
| - | ^ old | + | ^ old ^ new ^ |
| - | | git checkout < | + | | git checkout < |
| - | | git checkout < | + | | git checkout < |
| + | | git checkout < | ||
| The old checkout commands still work as always. | The old checkout commands still work as always. | ||
| + | (Note that checkout of a commit is extraordinary and results in a detached HEAD.) | ||
| ====diff==== | ====diff==== | ||
| - | git diff # before the add git add . | ||
| - | git diff HEAD # after the add | + | Use diff to find the differences between two branches or between two files.\\ |
| - | + | Note that there is a big difference between .. and ... between the two commit designations.\\ | |
| - | git diff -U0 # do not display context | + | Also the two hyphens |
| - | git diff -w # ignore whitespace | + | |
| + | |||
| + | git diff 1bde095..b0ba90d -- html/ | ||
| - | git diff -w --word-diff-regex=[^[: | + | By default diff compares all of the currently modified files. |
| + | |||
| + | git diff # before the add | ||
| + | git add . | ||
| + | git diff HEAD # after the add | ||
| + | |||
| + | Additional options. | ||
| + | |||
| + | git diff -U0 # do not display context | ||
| + | |||
| + | git diff -w # ignore whitespace | ||
| + | | ||
| In .gitconfig, add | In .gitconfig, add | ||
| [core] whitespace = -trailing-space, | [core] whitespace = -trailing-space, | ||
| + | |||
| ====log==== | ====log==== | ||
| Line 170: | Line 304: | ||
| git log --oneline -10 | git log --oneline -10 | ||
| - | git log --oneline --graph | + | git log --oneline --graph |
| - | git log --oneline --graph --decorate | + | ==== interactive rebase ==== |
| + | If you don't like the way the log looks now, the interactive rebase command lets you fix, reorder, drop and squash the commits, and reword the commit messages. | ||
| + | $ git switch mybranch | ||
| + | $ git rebase -i HEAD~3 | ||
| + | $ git rebase -i b619fca0 | ||
| + | |||
| + | The log is displayed in reverse chronological order. | ||
| + | |||
| + | The interactive rebase editor window displays the commits in chronological order. | ||
| + | |||
| + | A note about reordering the commits. | ||
| + | |||
| + | https:// | ||
| ===== How to use ===== | ===== How to use ===== | ||
| + | |||
| + | ==== Configuration ==== | ||
| + | |||
| + | Use git config --global to set name and email. | ||
| + | * Support anonymity by using a shielded email, like for example the private GitLab noreply masked email. | ||
| + | * Consider using a pair-programming persona as user.name to give credit to the AI author. | ||
| + | |||
| + | $ git config --global user.name "Your Name" | ||
| + | $ git config --global user.name "Your Name & Gemini 1.5 Flash" | ||
| + | $ git config --global user.email " | ||
| + | |||
| + | Aider automatically adds a Co-authored by: line to the body. Tell aider to skip that by adding parameters to .aider.conf.yml file: | ||
| + | attribute-author: | ||
| + | attribute-committer: | ||
| ====Create a new repository==== | ====Create a new repository==== | ||
git.1780798668.txt.gz · Last modified: 2026/06/06 22:17 by jhagstrand