logo

8. How to use git - Quick Start

8.1. How to open a terminal

Once logged on the CCOM/JHC server, a preliminary step to use git is the opening of a terminal (Fig. 8.1).

open terminal

Fig. 8.1 How to open a terminal on the CCOM/JHC server.

8.2. How to create a new local repository

To create a new local git repository (Fig. 8.2):

  • Create an empty folder with a name of your choice (e.g., mkdir my_repo).

  • From within the created folder (e.g., cd my_repo), execute git init.

new repo

Fig. 8.2 How to create a new git repository.

8.3. How to make the first commit

Before committing the code, you need to set name and email to identify who submitted a change:

git config --global user.email "my_email@example.com"
git config --global user.name "My Name"

Now an example of commit (Fig. 8.3). We first create a file (i.e., a README file) with a basic welcome message, then:

  • The created file is added to the tracked files with git add.

  • The above change to the repository is committed with git commit.

echo "Welcome to my repository!" >> README.rst
git add README.rst
git commit -m "First commit"
first commit

Fig. 8.3 Example of first commit.

8.4. How to set and push to a remote repository

You first need to create a repository on an git hosting services such as GitHub (Fig. 8.4), GitLab, and BitBucket.

Once that that a remote repository is created, an url similar to the following one will be available:

  • https://github.com/hydroffice/my_repo.git

github repo

Fig. 8.4 Example of remote repository created on GitHub.

You set the above url as the remote repository (this operation usually only happens once, at the creation time):

git remote add origin https://github.com/hydroffice/my_repo.git

Then, you push to the remote repository each time that you want to remotely store your changes:

git push -u origin master

You will be asked to authenticate yourself on the hosting service of your choice (Fig. 8.5).

push changes

Fig. 8.5 Example of how to push changes to a GitHub repository.

After that the push operation is completed, you should be able to visualize your updated code on the hosting service (Fig. 8.6).

github repo with commit

Fig. 8.6 Results on the GitHub repository after the push action for a first commit.

8.5. Other useful git operations

  • To display all the changes for both tracked and un-tracked files:

git status
  • To display changes only to tracked files:

git diff
  • To add a specific file (e.g., “Lecture0.ipynb”) to the tracking for the next commit:

git add Lecture0.ipynb
  • To commit all local changes in tracked files (-a) with a message (-m "Latest changes")

git commit -a -m "Latest changes"
  • To download all changes from a remote repository (e.g., “origin”):

git pull origin
  • To discard all the local changes to tracked files. You will lose all the unpushed changes!!!

git reset -hard HEAD

8.6. Useful references