Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

git temporary checkout that is ignored by file tracking

I have a script that checks out a branch to a temporary directory in order to compare it with the current work dir (e.g. using beyond compare). The temporary directory is deleted afterwards (i.e. does not affect the working dir unless I intentionally copy something over, for instance). The essential checkout command is:

git --work-tree="${TEMP_DIR}" checkout ${BRANCH} -- ${FILES}

$FILES may be . when I want all files.

Problem: When the temporarily checked-out branch contains files that are not present in the working dir, the tracker detects them anyway, and next time I call git status, I get a long list of files that are not-yet committed as "new" (green), but then at the same time as "not staged" / "deleted" (red).

When adding them with git add . and commit, they are eventually ignored, but I would prefer that they were just ignored during the temporary checkout in the first place, so that git status showed only changes in the actual work dir.

Is there any kind of "peek" checkout that just silently copies the content of a branch to somewhere, without affecting any status/tracker?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The reason this is occurring is that the .git directory contains the index, which is used to keep the state of the working tree's files. Therefore, you can't really change the working tree location without also changing the working tree location, since the index is intimately intertwined with those particular files.

If you want to create a temporary directory, you can either use git worktree add to add a new working tree (with its own index) in another location, provided that the branch you check out is not the same as the current one (a branch may be checked out in at most one working tree in a time).

If you just want to dump some files somewhere and don't need Git functionality at all, then you use git archive $BRANCH -- $FILES | tar -C "$TEMP_DIR" -xf - to create a tar archive of those files and extract it into that temporary directory.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...