Git defines a version control processes for how projects should be shared. Many websites exist that allow you to host your Git projects. GitHub is the most popular option, Bitbucket is another popular alternative, both allow free accounts that can create unlimited public or private repositories. GitLab offers a self-hosted alternative allowing for private development without relying on third-party hosting.
The process to first download a Git repository is called cloning. Cloning includes all the information about the repository at the time, however, to keep the repository up to date over time you need to pull down new versions. To do so, simply open a terminal window in the directory with the local version of the repository and type the command “git pull”.
The operation of this command is particularly simple if you’re just wanting to download an updated version of the repository; the local version will be updated to match the remote version. You may, however, see that there could be issues if you’ve made modifications to your local version.
The command “git pull” actually runs two separate commands “git fetch” and “git merge FETCH HEAD”. The sub-command “git fetch” specifically pulls down the newest version from the online repository and temporarily stores it. The sub-command “git merge FETCH HEAD” then merges your local changes with the downloaded version, with newer commits being preferred.
Tip: A “git commit” is a submission of a change, it’s possible to have multiple local and remote commits that do the same or different things. Local commits are not visible to the remote version until the changes are pushed. Each commit details exactly what changes were made and include a timestamp.
Conflicts between local and remote versions
Ideally in a merge scenario, there will be no conflicts and the merge process will complete automatically. Merges can be particularly easy if few changes were made, or if your local changes don’t interfere with any remote changes. If, however, there are conflicting complex changes to the same part of the code Git will throw a merge error.
At this point, you can either abort the merge with the command “git merge –abort” or try to manually resolve the conflicts. The commands “git mergetool” and “git diff” provide a graphical example of the differences that should help to make it as easy as possible to see what changes need to be manually applied to allow the merge to complete. Once you’ve resolved all of the conflicts, type the command “git merge –continue” to complete the merge.