I am new to git and recently get confused about the code below. My question are,
- Why
git branch new_branch
will copy the currentrelease_branch
? - After checkout the new branch, why we could enter a sub directory and just re-checkout an old branch to bring in the old script?
- Why the last checkout line (for old branch) will only restore the files in this subdirectory instead of bringing all scripts or switching to the old branch?
# Say pwd is /home/ab/
git checkout -f $RELEASE_BRANCH
git branch $NEW_REPAIR_BRANCH
git checkout $NEW_REPAIR_BRANCH
cd /home/ab/cd/ef/
rm -r *
git checkout $OLD_REPAIR_BRANCH *
Is there anyone could help me explain the logic? Much appreciate! Btw, I believe they should be able to be simplified as well!
CodePudding user response:
Why git branch new_branch will copy the current release_branch?
git branch new_branch
is short for git branch new_branch HEAD
. It creates new_branch
from the current HEAD. If HEAD
points to release_branch
, then it's equivalent to git branch new_branch release_branch
.
After checkout the new branch, why we could enter a sub directory and just re-checkout an old branch to bring in the old script?
Why the last checkout line (for old branch) will only restore the files in this subdirectory instead of bringing all scripts or switching to the old branch?
git checkout $OLD_REPAIR_BRANCH
means "to checkout $OLD_REPAIR_BRANCH". But git checkout $OLD_REPAIR_BRANCH *
is quite different. It does not switch the branch at all. *
means all files and directories under the current folder. The files and directories whose names start with a dot are excluded, like .git
and .gitignore
.
# enter /home/ab/cd/ef
cd /home/ab/cd/ef/
# remove all files and directories under /home/ab/cd/ef/
# files like `.gitignore` and folders like `.git` are not removed
rm -r *
git checkout $OLD_REPAIR_BRANCH *
update the files with their versions in OLD_REPAIR_BRANCH
. Imagine that it creates a file with its content recorded in OLD_REPAIR_BRANCH
. As *
refers to the entries under the current directory only, the files in its parent directories and sibling directories are not affected.