简体   繁体   中英

Dangers of 'git pull' in case the only changes to local repository come from the remote repository

I have a use-case where script needs to fetch a remote repository in order to get the latest revision of a specific file in this remote repository. Usual recommendations for pulling a remote repository are git checkout master to move to master branch, git remote update -p to fetch from remote repositories plus delete the branches which are no longer defined in the remote and finally git merge --ff-only @{u} to merge the changes is fast-forward is possible. However, do I need all this if my script never creates additional branches locally or never adds/commits anything. Am I correct, that in case the only changes to local repository come from the remote repository, then simply git pull is enough?

You do not need a "merge" operation (which is implied by "pull"). To check whether a file has been updated, you can use

git fetch origin refs/heads/master
git diff --quiet FETCH_HEAD..last-check -- the-file || echo "changed"
git branch -f last-check FETCH_HEAD

Answers for the updated question

The "last revision" (where the-file has been changed) in git sense is

git log --format=format:%H -1 FETCH_HEAD -- the-file

To get the content of the file, you can use

git show FETCH_HEAD:the-file

To replace the file (and only this one file) in your working directory, use

git checkout FETCH_HEAD -- the-file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM