Notes on GIT
I’m not using git
often enough to be confortable with uncommon utilizations. Here are some notes about what I’ve trouble to remember.
Acting on the worktree and the stage (aka index)
git restore files
copy files from the stage to the worktree
git restore –staged|-S files
copy files from the HEAD to the stage
git restore –staged|-S –worktree|-W files
copy files from the HEAD to both the stage and the work tree.
git add files
copy files from the work tree to the stage.
git commit
create a new commit from the stage.
Acting on the current branch
git reset –soft commit
change the commit of the branch, but does not change the stage nor the work tree.
git reset [–mixed] commit
change the commit of the branch, change the stage to the content of the commit but does not change the work tree.
git reset –hard commit
change the commit of the branch, the stage and the work tree.
Changing the branch
git switch branch
switch to branch.
git switch -c branch
create a new branch and switch to it.
git switch -c newbranch commit
create a new branch starting from commit
git switch –detach commit
switch to commit in detached state. If needed a new branch can be created later with
git switch -c branch
If the work tree of the stage are not equivalent to HEAD, additional options are needed to proceed:
–discard-changes
discard the stage and work tree
–merge
merge the current state and the target one.
Diff
git diff – file
difference between file and its staged version
git diff –staged – file
difference between the staged version of file and its committed version
git diff HEAD – file
difference between file and its committed version
--
can often be omitted if the file hasn’t the same name as a commit.
HEAD
gives the current branch (or for bare repository the default branch for new clones)
current value for the repository
git symbolic-ref --short HEAD
a checkout change HEAD for a normal repository. For a bare repository
git symbolic-ref HEAD refs/heads/mybranch
from inside the repository directory.
AFAIK there is no way to change it for a remote repository. Services like GitHub often allow to change it in the settings part of the interface.
remote/HEAD
When cloning a repository, there is a HEAD created in the remote branch which points to the HEAD branch at the time when you did the clone. AFAIK, git does not change it implicitly as part of any synchronization afterwards. You can change it with the command
git remote set-head remotename remotebranch
There are some options (-a
to get the current value from the remote, -d
to delete it).
It allows some commands (I use that possibility with log and diff) to refer to that branch with just the remote name.
Notes
git switch
and git restore
are intended to replace git checkout
and provides the same functionalities with a clearer use model.