Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Lab3: Remote synchronization and ignoring files

IUT d'Orsay, Université Paris-Saclay

Objectives

The goal of this lab is to understand the following concepts and commands:

Version history graph

To better understand this concept, we will look at the version history graph of our current project.

  1. Run git log --oneline to display the repository history where each commit corresponds to one line starting with a code, called the commit hash, allowing the commit to be identified, followed by the commit message.

  1. Run git log --oneline --graph to see the graph (drawn in ASCII in the terminal) associated with these commits, where each vertex is represented by a star.

Your graph should be linear (a path).

When you learn how to manage multiple branches in your repository, you will be able to add the options --decorate (to display the branch name next to the commits) and --all (to see all branches) to the command git log --oneline --graph.

  1. Define the new command git graph as an alias as follows:

git config --global alias.graph "log --oneline --graph --decorate --all"
  1. Run git graph.

Diff and Pull

Go to the Remote Repo on GitLab.

  1. Click on Code then Web IDE in Open with.

  2. Create a new directory Lab3/ and a file hello-world.cpp with the following code.

hello-world.cpp
#include <iostream>
using namespace std;
int main() {
    cout << "Hello World!" << endl;
    return 0;
}

You can see a 1 on the left sidebar, indicating that a change has been made. If you click on it, you will see a change related to the file hello-world.cpp and the option to save and publish to the main branch (Commit and push to ‘main’).

  1. Add a commit message.

  2. Click the Commit and push to ‘main’ button.

  3. Click on Continue when asked if you want to save to the default branch (main).

Return to your Local Repo and suppose that a collaborator made changes, then published them to the Remote Repo without informing you.

  1. Run git fetch, then git status.

  1. Run git diff origin/main to see the differences between the two repositories.

  1. To download the changes from the Remote Repo that are not yet on your Local Repo, run git pull.

Show

  1. Run git graph to see the history graph of your project.

  1. Choose a commit hash from a past commit and run git show <commit hash>. Observe.

For example:

git show acbd123
  1. To see a specific file in a past commit, run git show <commit hash>:<file>.

For example:

git show acbd123:Lab1/my-first-file.txt

Ignoring files

  1. Open the directory Lab3/ where the file hello-world.cpp is located.

  2. Compile the code with g++ -o <file name> <file name>.cpp.

For example:

g++ -o hello-world hello-world.cpp
  1. Run the executable hello-world with the command ./hello-world.

Executables are part of the files that we want to ignore.

  1. Create the .gitignore file.

  1. Write the name of the executable hello-world in .gitignore.

  1. Run git add .gitignore, git commit -m "<message>" and git push to send the .gitignore file to the Remote Repo.

  2. Run git status. What changes compared to usual?

  1. Replace the code in the .gitignore file with the following code.

*
!*.*
!*/
  1. Run add, commit, and push to synchronize your repositories.

Removing files

  1. Open the directory Lab3/ and create a file to-be-removed.txt.

  2. Add, commit, and push this file to the Remote Repo.

  1. Remove the file to-be-removed.txt and synchronize with the Remote Repo.

In your Local Repo and Remote Repo, you should now have the following structure (without considering the hidden .git which must be present in all projects tracked by Git):

Lab1/
├── my-first-file.txt
├── my-second-file.txt
Lab2/
├── <a text file>.txt
Lab3/
├── hello-world.cpp
.gitignore

Other files may be present in your Working Tree but make sure they are ignored (in .gitignore and then with git status).

  1. If you have unwanted files, remove them and synchronize with the Remote Repo.

  1. Return to the objectives and check the points that you have mastered. Practice the commands and concepts that you do not yet fully understand. Ask your instructor for help if needed.