Collaborative development processes, documentation, and project management in companies cannot be imagined without such technology as a repository version control system. It allows instead of the usual solutions with mutex and similar mechanisms for distributed work on a single project.
Classically, the scheme looks like this, a user accesses a resource and requests access to a file, then works and saves it in the same resource. However, if two users worked together, the one who wrote the last changes would overwrite the work of the previous user and the material would be lost. Linus Torvalds developed Git to solve this problem! It allows everyone to save their local copy on their device and then upload their versions to a single repository in snapshot format.
One of the processes for working with repositories is managing and working with remote repos, in this article we will look at the difference between the pull, fetch and clone commands.
What is the difference between Git commands?
Recall that git works as follows: the user works in an environment called Working Tree, where the repository and working files are stored. When changes are made with the git add . command, the current state of the working directory or Working Tree is recorded. The files are then archived or committed and sent to the repository.
But the reverse process looks a bit different and the download itself can be represented, according to the diagram below.
The three git commands pull, fetch and clone are responsible for working on downloading resources from a remote repository or remote repo. But for different scenarios it will be convenient to use one of them.
Git clone
Let's imagine that your colleague has run the git push command and just uploaded a fresh version of the repository to the remote repo. Then you want to download it without affecting your copy in Working Tree, or you don't have one, so the git clone command will work for you. For example the command:
It will download the files without changing your current workspace, and even if you accidentally make a mistake and download a copy to the same directory as your repo, git will error out.
For correct operation, move to another directory or delete an existing repository, then repeat the command and wait for the download. This method downloads both the repository and extracts files from it, then can be modified and uploaded with modifications back to the remote repo. However, if you need to work with branches and different versions of commit, then the git checkout command is perfect for such purposes.
Git pull
This command is designed to update data in an existing repository by merging it with the current local repository. It downloads only different files from the main repository and uploads them directly to the working directory. For example, one of your users has committed changes that are important to your work. To do this, select the desired branch with the git checkout <branch> command and download the changes:
If you see a message about divergent branches - it means that you have not chosen the way to work with commits: merge or rebase. In order for your work and commit to go through merge, write the command:
But if you want to keep your current work and not merge, then use rebase:
After that the changes will be committed and reflected in the working directory. But, what if you want to see the updates that will be made to the files before the merge? There is a fetch command for that!
Git fetch
The principle of operation is similar to the git pull command, only in this case the changes are first uploaded to the local repository and then the user decides whether to merge with the working directory. The mechanism is based on the fact that the local repository also stores remote branches. These are where the updated commit is written to, and then git rebase or git merge is used to merge them.
After this action, the updates are written to remote branches and are waiting for further merging, or for the update to be cancelled. As indicated in the screenshot above, the changes are saved to the origin/main branch, so let's go to it with the command.
At the very bottom are updates for the branch compared to the current working directory, to view the changes type ls in the working folder. To merge folders, type the command:
After that the working directory will be updated and it will be possible to continue working with the updated files from the remote repo.