diff --git a/03-cli-basics/README.md b/03-cli-basics/README.md index 1deedcd40d5111c03278ade7eccf7f631516fd19..3e78c81092c68d62a99e898d2be5f95094b140e0 100644 --- a/03-cli-basics/README.md +++ b/03-cli-basics/README.md @@ -112,6 +112,45 @@ On GitLab, the project welcome page displays information about the latest commit  +## Keeping a local repository synchronized with a remote repository + +A same remote repository can be cloned several times: by different people on each of their machines, by a single user on different machines, by a single user in different directories on the same machine. + +When someone pushes modifications from a clone (local repository) to the original remote repository, *these modifications do not get pushed to every existing clone*. It is the duty of the owner of each local repository to ensure that the local copy does not get too far behind the remote repository. + +### Fetching information about the remote repository + +The `git fetch` [command](https://git-scm.com/docs/git-fetch)] retrieves information about all modifications made to the current branch of the remote repository, since the local repository was last synchronized. *It does not modify the local repository.*". + +After completing a fetch operation, doing `git status` will display how many commits have to be "replayed" in the local repository in order to synchronize it again with the remote repository. + +The following example screenshot shows the output of a `git log` on a local repository: + +As can be seen, the most recent commit known to the local repository dates from Nov 16 (and has an id starting with `3367`) + +The following screenshot shows the curent state of the remote repository on GitLab : + +Notice that in the remote repository, there is an additionnal commit, dating from Nov 18 (with an identifier starting with `8de7`) + +The local repository is thus one commit behind the remote repository. The following screenshot displays the output the a `git fetch` command in the local repository: + + +The `git status` command now displays the difference (expressed in the number of commits) between the current local repository and the remote repository it was cloned from. + + +### Applying remote changes to the local repository +Actually synchronizing the local repository, with the information retrieved through `git fetch` is performed with the `git pull` (command)[(https://git-scm.com/docs/git-pull)]. Which has the following result on our example: + + +### Important notes +It would have been possible to run `git pull` without running `git fetch` before. This would have fetched the remote commits and applied them to the local repository in a single step. Running `git fetch` beforehand lets you check how far behind your local repository is before trying to apply the changes with `git pull` (which is a proxy for the risk of having to manage conflicts when applying the changes, more on that later). Running `git pull` directly is OK if you know you're not far behind the remote repository. + + + + + + + diff --git a/03-cli-basics/exercises.md b/03-cli-basics/exercises.md index a2d74cf8993d0a2dbfdfd854e0093c9bbddde3d8..913e3594beadcdd7ba196b9b50e6c4ee5a6be03a 100644 --- a/03-cli-basics/exercises.md +++ b/03-cli-basics/exercises.md @@ -26,4 +26,13 @@ 7. Commit all your latest changes. 8. After checking all went well, propagate the changes to the remote GitLab project. +## Synchronize your local repository afer making changes to the remote repository + 1. Using the GitLab Web interface, make some changes to your remote repository (add an entry to the `.md` file, and add an image). + 1. On your workstation, in your local repository: + 1. Fetch the latest commit(s) from the remote repository. + 2. Check that your local repository is at least one commit behind. + 3. Synchronize your local repository with the latest commits from the remote repository. + + + diff --git a/03-cli-basics/media/git_fetch.png b/03-cli-basics/media/git_fetch.png new file mode 100644 index 0000000000000000000000000000000000000000..90299dacca651a9349dda46e9b31b4db3e4735d0 Binary files /dev/null and b/03-cli-basics/media/git_fetch.png differ diff --git a/03-cli-basics/media/git_log_before_fetch.png b/03-cli-basics/media/git_log_before_fetch.png new file mode 100644 index 0000000000000000000000000000000000000000..d3070b95871d587253508ab19f910137ac0a1bee Binary files /dev/null and b/03-cli-basics/media/git_log_before_fetch.png differ diff --git a/03-cli-basics/media/git_pull.png b/03-cli-basics/media/git_pull.png new file mode 100644 index 0000000000000000000000000000000000000000..d1a6f92ce14e71e12fc19695ff83b36d0de020db Binary files /dev/null and b/03-cli-basics/media/git_pull.png differ diff --git a/03-cli-basics/media/git_status_after_fetch.png b/03-cli-basics/media/git_status_after_fetch.png new file mode 100644 index 0000000000000000000000000000000000000000..c6e23ae4071c223087c72b7179bbe3ef8d433682 Binary files /dev/null and b/03-cli-basics/media/git_status_after_fetch.png differ diff --git a/03-cli-basics/media/gitlab_log_latest.png b/03-cli-basics/media/gitlab_log_latest.png new file mode 100644 index 0000000000000000000000000000000000000000..f88aba2b1edb08d3c0081c22450b9aac3e9fe651 Binary files /dev/null and b/03-cli-basics/media/gitlab_log_latest.png differ