2

I am new to git and recently get confused about the code below. My question are,

  • Why git branch new_branch will copy the current 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?
# 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!

GuoLY96
  • 25
  • 3
  • You might want to stop using `git checkout` entirely: consider using the two commands `git switch` and `git restore` instead. You will need Git version 2.23 or later. Moving to the new commands may help you understand why `git checkout` is so puzzling: it's actually two separate commands that are just spelled the same! – torek Jun 30 '22 at 12:42
  • @torek Agree, the code I attached is pretty old. Will try to update it. – GuoLY96 Jun 30 '22 at 13:35

2 Answers2

1

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 * updates 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.

ElpieKay
  • 27,194
  • 6
  • 32
  • 53
0

Anything done with git checkout/git branch could be done with the new git switch command (man page), as well as git restore (for file-based operations: man page).

git switch -c $NEW_REPAIR_BRANCH $RELEASE_BRANCH
git restore --source $OLD_REPAIR_BRANCH

That way, you clearly distinguish the branch switches from the files restore steps.
And you avoid '*', which depends on the underlying shell to be interpreted and expanded.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250