Basic commands¶
This section will focus on some of the basic commands of Git and how to use them.
Getting help¶
$ git help <command>
$ man git-<command>
$ git help commit
GIT-COMMIT(1) Git Manual GIT-COMMIT(1)
NAME
git-commit - Record changes to the repository
SYNOPSIS
git commit [-a | --interactive | --patch] [-s] [-v] [-u<mode>] [--amend]
[--dry-run] [(-c | -C | --fixup | --squash) <commit>]
[-F <file> | -m <msg>] [--reset-author] [--allow-empty]
[--allow-empty-message] [--no-verify] [-e] [--author=<author>]
[--date=<date>] [--cleanup=<mode>] [--[no-]status]
[-i | -o] [-S[<keyid>]] [--] [<file>...]
DESCRIPTION
How Git Organizes Your Project¶
Git views your files through four main areas:
- Working Directory: the files currently visible and editable on your computer.
- Staging Area: a temporary space where selected changes are prepared before saving.
- Local Repository: the project history stored in the hidden
.gitdirectory on your machine. - Remote Repository: an online copy of the project hosted on services such as GitHub or GitLab.
Common Git Vocabulary¶
| Term | Description |
|---|---|
| commit | A recorded snapshot of your project |
| repository (repo) | A project managed by Git, including its history |
| branch | A separate line of development |
| merge | Combining work from different branches |
| HEAD | A reference to your current commit |
| remote | A repository stored elsewhere online |
| clone | A local copy of a remote repository |
Creating a repository from scratch¶
In case you want to start a project from scratch called myproject:
this will create a folder called .git in the current folder which contains the Git-related files.
We can now ask about the status of the repository:
$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
Creating a repository by cloning an existing repository¶
Use the command:
repository_location is the path of the Git repository (if it is in your local machine) or a URL if it is on the internet. path_where_it_will_be is the path for the cloned repository.
Downloading an Existing Repository¶
To copy an existing project from a hosting platform:
This downloads the project and its entire history to your machine. Note that since you used https://... you cannot push to the remote repository. Github only allows that with SSH.
$ git clone https://github.com/aliceuser2020/my-first-project.git ./my-project
Cloning into 'GitCourse/Alice/my-project'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
Checking connectivity... done.
$ cd ./my-project
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
Checking Repository Status¶
The git status command displays:
- modified files
- staged files
- current branch information
Example:
Creating Your First Commit¶
The standard Git workflow follows three steps:
- Modify files
- Stage changes
- Commit changes
Create a file¶
Stage the file¶
Save the snapshot¶
Commit messages should explain the purpose of the change clearly and concisel.
Staging Multiple Files¶
# Add one file
git add file.txt
# Add files matching a pattern
git add *.txt
# Stage everything
git add -A
# Commit tracked files directly
git commit -a -m "Update project files"
git commit -aonly affects files already tracked by Git.
Tracking and Saving Changes¶
Inspecting Changes¶
Use git diff to compare file versions.
# Unstaged changes
git diff
# Staged changes
git diff --staged
# Compare against latest commit
git diff HEAD
+indicates added lines-indicates removed lines
Viewing Commit History¶
Browse previous commits with git log.
Compact format:
Graphical history:
Example:
Renaming and Moving Files¶
Use Git’s built-in move command so history remains intact:
Move a file into another folder:
These actions are automatically staged.
Deleting Files¶
This removes the file and stages the deltion simultaneously.
Ignoring Unwanted Files¶
Some files should never be committed, such as:
- temporary files
- logs
- build outputs
- environment secrets
Create a .gitignore file:
Commit it like any other file:
Reverting Changes¶
Remove a file from staging¶
Discard local edits¶
This permanently removes uncommitted modifications.
Undo the most recent commit safely¶
Git creates a new commit that reverses the previous one.
Creating Command Aliases¶
Frequently used commands can be shortened:
Now you can simply run:
Exercises¶
Try out the basic commands¶
The purpose of this exercise is to learn about basic commands further.
- Create a folder called “mysecondrepo” which will contain your repository.
Tasks:
- initialize the repository
- create a README.md file and add this text on the first line: “File created on Sep. 30, 2020”
- stage the file
- commit the changes
- create a second file “Poem.md” without any text (hint: use touch command), stage and commit it
- Copy and paste the two paragraphs below of the poem la lluvia by Jorge Luis Borges into the Poem.md file:
Tasks:
- stage the first paragraph only
- confirm that the file has been partially staged by checking the status.
- check if the staged changes correspond to just the first paragraph by looking at the differences between: – the working directory vs. the staging area – the staging area vs. the committed changes region (Repo)
- commit the staged changes and check the status
- stage the second paragraph and commit the changes
Bruscamente la tarde se ha aclarado
Porque ya cae la lluvia minuciosa.
Cae o cayó. La lluvia es una cosa
Que sin duda sucede en el pasado.
Quien la oye caer ha recobrado
El tiempo en que la suerte venturosa
Le reveló una flor llamada rosa
Y el curioso color del colorado.
4. (Challenge task) Add the next paragraph of the poem:
Esta lluvia que ciega los cristales
Alegrará en perdidos arrabales
Las negras uvas de una parra en cierto
Patio que ya no existe. La mojada
Tarde me trae la voz, la voz deseada,
De mi padre que vuelve y que no ha muerto.
stage the changes and commit them. Now, we have the last four commits refering to the same topic (same poem). The goal of this part of the exercise is to summarize those four commits into a single one.
hint: git rebase -i HEAD~4
5. End of exercise
git diff usage¶
The purpose of this exercise is to show you the usage of the git diff command.
- Create a folder called “myfirstrepo” which will contain your repository.
Tasks:- initialize the repository
- create a README.md file and add this text on the first line: “File created on Sep. 30, 2020”
- stage the file
- commit the changes
At this point, your working directory, the staging area, and the local repository (committed changes) are all synchronized.
- Make a change to the README.md file by adding a line with the string “TODO list”. Now, the staging area and the local repository are synchronized with each other but the working directory is not synchronized with them.
- check the differences between the working directory and the staging area (hint: git diff)
- check the differences between the working directory and the local repository (hint: git diff HEAD)
confirm that the changes correspond to what you expected by looking at the diff outputs.
- Add the file README.md to the staging area. Now, the working directory and the staging area are synchronized with each other but they are not synchronized with the local repository.
- check the differences between the staging area and the local repository (hint: git diff –staged)
- check the differences between the working directory and the local repository (hint: git diff HEAD)
confirm that the changes correspond to what you expected by looking at the diff outputs.
- Make an additional modification to the README.md file by adding a line with the string “Add the support email”.
Now, all the three areas working directory, the staging area and the local repository are not synchronized with each other.
- check the differences between the working directory and the staging area (hint: git diff)
- check the differences between the staging area and the local repository (hint: git diff –staged)
- check the differences between the working directory and the local repository (hint: git diff HEAD)
confirm that the changes correspond to what you expected by looking at the diff outputs.
- End of exercise