Summary of Git terms that tend to cause confusion

Git, a distributed version control system, has continued to increase its share since its introduction in 2005, and according to
Confusing git terminology
https://jvns.ca/blog/2023/11/01/confusing-git-terminology/
◆HEAD and 'heads'
HEAD refers to the branch or commit you are currently checking out, and is stored in '.git/HEAD'. On the other hand, what is saved in '.git/refs/heads' is a branch, and 'heads' can be read as 'branches'.
◆detached HEAD
In addition to branches, Git allows you to check out tags and commits. If you check out something other than a branch, it will be in a 'detached HEAD' state, and in this state, there is no branch, so 'git pull' and 'git push' basically do not work, and 'git commit', 'git merge', ' git rebase' and git cherry-pick can be used, but they remain separate from the branch, making it difficult to find commits.
You can escape the detached HEAD state by creating a new branch or switching to an existing branch.
◆Meaning of “ours” and “theirs” during merge/rebase
If a conflict occurs during the merge, you can select the file on the 'ours' side with 'git checkout --ours file.txt'. In this case, 'ours' refers to the branch you are currently checking out, and 'theirs' refers to the branch you are merging from.
[code]$ git checkout merge-into-ours # Branch to check out is 'ours'
$ git merge from-theirs # The branch specified for merge is 'theirs'[/code]
On the other hand, in a rebase, the behavior is reversed.
[code]$ git checkout theirs # The branch to check out is 'theirs'
$ git rebase ours # The branch specified for rebase is 'ours'[/code]
◆'Your branch is up to date with 'origin/main''
A simple interpretation of this message would be to mean 'Your branch is the latest on 'origin/main',' but it actually means 'your branch is the latest on 'origin/main',' but in reality it is It means the latest on '/main'. If you haven't done a ``git pull'' or ``git fetch'' in a while, it can give your users a false sense of security when in fact they're not up to date.
◆'HEAD^' 'HEAD~' 'HEAD^^' 'HEAD~~' 'HEAD^2' 'HEAD~2'
A summary of these is as follows.
・“HEAD^” and “HEAD~” have the same meaning, meaning the previous commit
・“HEAD^^^”, “HEAD~~~” and “HEAD~3” have the same meaning, meaning the three commits before.
・“HEAD^3” means the third parent commit, and has a different meaning from “HEAD~3”
Most commits have only one parent, but some commits, such as merge commits, have more than one parent. In a commit with multiple parents, 'HEAD^○' is used when you want to specify the '○th parent'.
◆'..' and '...'
Both '..' and '...' are used to specify a range. For example, as shown in the figure below, consider a case where commits A and B exist on the main branch, and commits C and D exist on the test branch branched from A.

At this time, if you specify the following ranges in 'git log', the results will be as follows.
・'main..test'
Contains commits C and D.
・'test..main'
Contains commit B.
・'main...test'
Contains commits B, C, and D.
On the other hand, the range specification in ``git diff'' is different from that in ``git log'', and is as follows.
・'test..main'
All the differences in both trees, that is, the differences between B and D, are displayed.
・'test...main'
The difference in one tree, that is, the difference between A and D, is displayed.
◆“can be fast-forwarded”
A state where fast-forwarding is possible is, for example, a state where only one branch has new commits, as shown in the image below.

The appearance is as shown below. You can merge by just adding two commits, D and E, and there is no chance of any conflicts or other issues.

On the other hand, if commits are added to each branch as shown in the image below, fast forwarding is not possible.
◆ 'reference' 'symbolic reference'
Git has at least three things called 'references':
・Branch and tag
・HEAD
- Something that points to a commit ID like 'HEAD^^^'
The only symbolic reference that Evans has ever used is HEAD, and he does not understand why he would generalize using the term ``symbolic reference.''
◆refspecs
The following information is stored in 'refspecs', which shows information on reference sources and destinations, but Evans stated that there is no motivation to do anything other than add it using 'git clone' or 'git remote add'. I am.
◆“tree-ish”
For example, if you look at the ``git checkout'' manual, you will see the word ``<tree-ish>'' as shown below.
[code]git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>] [--]
The following three things apply to this <tree-ish>.
・Commit ID (e.g. '182cd3f')
・Reference to commit ID (e.g. 'main', 'HEAD^^', 'v0.3.2')
・Subdirectories within commits (e.g. 'main:./docs')
However, Mr. Evans says that he has never used subdirectories within a commit, and it seems okay to think of them simply as commit IDs or references to commit IDs.
◆'index' 'staged' 'cached'
These three words all refer to the same thing: '.git/index'. '.git/index' stores files added using 'git add'.
◆'reset' 'revert' 'restore'
'git reset --hard' and 'git restore .' have the same behavior, but 'git reset --hard COMMIT' and 'git restore --source COMMIT .' have completely different behavior. ” “revert” and “restore” are words that can be quite confusing. A brief explanation of each movement by Mr. Evans is as follows.
・``git revert COMMIT''
Create a new commit that cancels the contents of COMMIT.
・``git reset --hard COMMIT''
This is a dangerous operation that deletes all changes after the COMMIT and forcefully reverts to the state at the time of the COMMIT.
・``git restore --source=COMMIT PATH''
Returns the file specified by PATH to its state at the time of COMMIT. It does not change any other files or commit history.
◆'untracked files' 'remote-tracking branch' 'track remote branch'
Git uses the term 'track' in three different senses.
・Untracked files
'Untracked files' in the 'git status' display means that the files are not managed by Git.
・remote-tracking branch
A local branch that tracks a remote, such as 'origin/main'. Unlike regular branches, you cannot create commits on them.
・track remote branch
'Track remote branch' is used to say 'This branch is tracking that remote branch.' For example, if the main branch is tracking a specific remote branch, when you execute a push or pull on the main branch, Automatically send and receive changes to tracked remote branches.
◆checkout
checkout is used in two completely different meanings.
・``git checkout BRANCH''
Switch branches.
・``git checkout file.txt''
Discard unstaged changes in file.txt.
Git developers are also aware of this issue, and checkout has been split into two commands: 'git switch' and 'git restore'.
◆reflog
Instead of 're-flog', 'ref-log' provides a history of 'references' such as branches, tags, HEAD, etc. This is useful if you accidentally delete an important branch.
◆merge vs rebase vs cherry-pick
The functions of each are as follows.
・merge
A merge creates one commit that merges two branches.
・rebase
Copies commits from the current branch to the target branch one by one.
・cherry-pick
It's similar to rebase, but it copies commits from the branch you picked from to the current branch, and the syntax is very different.
◆rebase –onto
Consider the following branch and commit situation.

At this time, by executing the command ``git rebase --onto main otherbranch mybranch'', you can move the F and G commits to the top of the main branch. In addition, Mr. Evans states that he has never encountered a situation like this.
There are still many confusing terms in Git that even Evans doesn't understand. There is also a post that explains how Git terminology is related to CVS and Subversion terminology, so if you are interested, please check it out.
Related Posts:
in Software, Posted by log1d_ts