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).
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
), executegit init
.
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"
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
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).
After that the push
operation is completed, you should be able to visualize your updated code
on the hosting service (Fig. 8.6).
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