# GIT
# branching
Make a new branch
git branch newbranch
# checkout vs switch
git checkout newbranch
switches to newbranch
.
! but
If you modify a file but haven't staged the change, then git checkout <filename>
will reverse the modifications... a quick and easy way to cancel changes to a file. You remain in the same branch.
Two completely different purposes, which could lead to confusion if a file name and a branch name are similar.
Having it as two commands is clearer.
checkout
has been divided to switch
and restore
.
git switch newbranch
is the newer command.
git switch -c newbranch
lets you create and switch to a new branch.
# merge
If you want to merge bugFix
branch to main
, switch to main
then use git merge bugFix
.
# rebasing
Rebasing essentially takes a set of commits, "copies" them, and plops them down somewhere else.
While this sounds confusing, the advantage of rebasing is that it can be used to make a nice linear sequence of commits. The commit log / history of the repository will be a lot cleaner if only rebasing is allowed.
Changing commit messages in direct form even if it was developed in parallel fashion.
# Relative Refs
Git log is used to identify each hash. The upside is that Git is smart about hashes. It only requires you to specify enough characters of the hash until it uniquely identifies the commit. So I can type
fed2
instead of the long string above.
- Moving upwards one commit at a time with caret operator
^
- Moving upwards a number of times with
~<num>
git switch master^
# ~ tilde operator
It might be tedious to type
^
several times, so Git also has the tilde (~) operator.
# -f force a checkout
You can pass the
-f
or--force
option with thegit checkout
command to force Git to switch branches, even if you have un-staged changes (in other words, the index of the working tree differs fromHEAD
). Basically, it can be used to throw away local changes.
# reset and revert
Reset is used to go back in local environment.
In a shared environment use revert
.