Overview
Teaching: 10 min
Exercises: 15 minQuestionsObjectives
- What is Git?
- What is a repository?
- How does Git operate?
- How do I make commits?
- How do I select what to commit?
- Learn to create Git repositories and make commits.
- Get a grasp of the structure of a repository.
- Learn how to inspect the project history.
- Learn how to write useful commit log messages.
.git
..git
, we remove the repository and history (but keep the working directory!)..git
uses relative paths - you can move the whole thing somewhere else and it will still work$ git add somefile.txt
$ git commit
$ git add file.txt anotherfile.txt
$ git commit
git add
, we “stage” the change), then shoot (git commit
):What do you think will be the outcome if you stage a file and then edit it and stage it again, do this several times and at the end perform a commit? (think of focusing several scenes and pressing the shoot button only at the end)
Also see instructions here
$ git config --global user.name "Your Name"
$ git config --global user.email yourname@example.com
$ git config --global core.editor your-editor
your-editor: vim, emacs, nano, any other of your choice
We will learn how to initialize a Git repository, how to track changes, and how to make delicious guacamole!
This example is inspired by Byron Smith.
Let us start. One of the basic principles of Git is that it is easy to create repositories:
$ mkdir recipe
$ cd recipe
$ git init
That’s it! We have now created an empty Git repository.
We will use git status
a lot to check out what is going on:
$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
We will make sense of this information during this workshop.
Let us now create two files.
One file is called instructions.txt
and contains:
* chop avocados
* chop onion
* squeeze lime
* add salt
* and mix well
The second file is called ingredients.txt
and contains:
* 2 avocados
* 1 lime
* 2 tsp salt
As mentioned above, in Git you can always check the status of files in your repository using
git status
. It is always a safe command to run and in general a good idea to
do when you are trying to figure out what to do next:
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
ingredients.txt
instructions.txt
nothing added to commit but untracked files present (use "git add" to track)
The two files are untracked in the repository (directory). You want to add the files (focus the camera) to the list of files tracked by Git. Git does not track any files automatically and you need make a conscious decision to add a file. Let’s do what Git hints at and add the files:
$ git add ingredients.txt
$ git add instructions.txt
$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: ingredients.txt
new file: instructions.txt
Now this change is staged and ready to be committed.
Let us now commit the change to the repository:
$ git commit -m "adding ingredients and instructions"
[master (root-commit) aa243ea] adding ingredients and instructions
2 files changed, 8 insertions(+)
create mode 100644 ingredients.txt
create mode 100644 instructions.txt
Right after we query the status to get this useful command into our muscle memory:
$ git status
What does the -m
flag mean? Let us check the help page for that command
(to get out of the help mode press ‘q’):
$ git help commit
You should see a very long help page as the tool is very versatile. Do not worry about this now but keep in mind that you can always read the help files when in doubt. Searching online can also be useful, but choosing search terms to find relevant information takes some practice and discussions in some online threads may be confusing. Note that help pages also work when you don’t have a network connection!
Now try git log
:
$ git log
commit 49baa1f9a7be6422090c4a8a10694a6708c2ff41 (HEAD -> master)
Author: gosia olejniczak <gosia.olejniczak@gmail.com>
Date: Tue Jan 28 08:39:07 2020 +0100
adding ingredients and instructions
We will use them when comparing versions and when going back in time.
git log --oneline
only shows the first 7 characters of the commit hash and is good to get an overview.git log --stat
is nice to show which files have been modified.Exercise: record changes
Add 1/2 onion to
ingredients.txt
and also the instruction to “enjoy!” toinstructions.txt
. Do not stage the changes yet.When you are done editing the files, try
git diff
:$ git diff
diff --git a/ingredients.txt b/ingredients.txt index 2607525..ec0abc6 100644 --- a/ingredients.txt +++ b/ingredients.txt @@ -1,3 +1,4 @@ * 2 avocados * 1 lime * 2 tsp salt +* 1/2 onion diff --git a/instructions.txt b/instructions.txt index 3eda565..f7dd63a 100644 --- a/instructions.txt +++ b/instructions.txt @@ -3,4 +3,4 @@ * squeeze lime * add salt * and mix well +* enjoy!
Now first stage and commit each change separately (what happens when we leave out the
-m
flag?):$ git add ingredients.txt $ git commit -m "add half an onion" $ git add instructions.txt $ git commit # <-- we have left out -m "..."
When you leave out the
-m
flag, Git should open an editor where you can edit your commit message. This message will be associated and stored with the changes you made. This message is your chance to explain what you’ve done and convince others (and your future self) that the changes you made were justified. Write a message and save and close the file.When you are done committing the changes, experiment with these commands:
$ git log # show commit logs $ git show # show various types of objects $ git diff # show changes
Using git log --oneline
we understand that the first line of the commit message is very important.
Good example:
increase threshold alpha to 2.0
the motivation for this change is
to enable ...
...
Convention: one line summarizing the commit, then one empty line, then paragraph(s) with more details in free form, if necessary.
Good references:
As a general rule compiled files are not committed to version control. There are many reasons for this:
For this we use .gitignore
files. Example:
# ignore compiled LaTeX files
*.log *.out *.pdf
# ignore compiled python 3 files
__pycache__
# ignore everything under build directory
build/
More about the syntax used to determine files to ignore is in the documentation.
You can have .gitignore
files in lower level directories and they affect the paths
relatively.
.gitignore
should be part of the repository (why?).
git status
a lot.Now we know how to save snapshots:
$ git add <file(s)>
$ git commit
Every state is then saved and later we will learn how to go back to these “checkpoints” and how to undo things.
$ git init # initialize new repository
$ git add # add files or stage file(s)
$ git commit # commit staged file(s)
$ git status # see what is going on
$ git log # see history
$ git diff # show unstaged/uncommitted modifications
$ git show # show the change for a specific commit
$ git mv # move tracked files
$ git rm # remove tracked files
Git is not ideal for large binary files (for this consider http://git-annex.branchable.com).
Test your understanding
Which command(s) below would save the changes of
myfile.txt
to my local Git repository?
$ git commit -m "my recent changes"
$ git init myfile.txt $ git commit -m "my recent changes"
$ git add myfile.txt $ git commit -m "my recent changes"
$ git commit -m myfile.txt "my recent changes"
Solution
- Would only create a commit if files have already been staged.
- Would try to create a new repository.
- Is correct: first add the file to the staging area, then commit.
- Would try to commit a file “my recent changes” with the message myfile.txt.
Here we worked via command line, but it is also possible to work from within a Git graphical user interface (GUI):
Key Points
Initializing a Git repository is simple:
git init
Commits should be used to tell a story.
Git uses the .git folder to store the snapshots.