If you delete the file locally in the cloned repository, git fetch does not restore the deleted file. There is a checkout command to restore. Do I understand correctly that git fetch does not download lost information because the remote repository stores information that the file was transferred earlier, but there is no information about its loss? I want a full understanding of this issue

  • What do you mean by "delete locally"? Delete on disk or delete on disk + commit delete to local repository? - Enikeyschik
  • Deleting a file or directory with the git rm command without a commit - Leonid Elkin

2 answers 2

git pull is git fetch + git merge .

git fetch updates the local data with data from the repository ( fetch is the actual command to update), but the local pointer remains in the same place where it was before.

Those. Yes, your understanding is generally correct. In the remote database there is no information about deleting the file and in fact, if there have not been commits to the database since the last pool / git fetch nothing happens in the git fetch process. git status will show you that

 $ git status On branch master Your branch is up to date with 'origin/master'. Changes to be committed: (use "git reset HEAD <file>..." to unstage) deleted: file.txt 

Those. your local database is synchronized with the remote one and there is one change without a commit.

If there were changes in the remote database, then after git fetch will be something like this:

 $ git status On branch master Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded. (use "git pull" to update your local branch) Changes to be committed: (use "git reset HEAD <file>..." to unstage) deleted: file.txt 

And git checkout generally from a slightly different area.

    You get it wrong. Should understand the terms

    • local storage (the contents of the .git folder where all changes are stored on all branches),
    • remote storage - similarly, only on another machine
    • working copy - source files you are working with - what is on the same level with the .git folder and below

      git fetch only pulls the database from the remote repository; it does not do any actions with a working copy.

    • In my understanding, your answer does not contradict what I said, with the exception of terms. To paraphrase my question, closer to your terms, I say that git fetch pumping out the database and storing information about what was received. Removing a file from the local one, no information about this before its direct sending via git push is received by the server and that is why re-executing git fetch will not work with lost files. - Leonid Elkin
    • @ Leonid Elkin Yes, it is. - Enikeyschik