How do I get set up to use Git?

When we use Git on a new computer for the first time, we need to configure a few things. Below are a few examples of configurations we will set as we get started with Git:

$ git config --global user.name "Your name"
$ git config --global user.email "user@email.com"
$ git config --global color.ui true

$ mkdir git_tutorial
$ ls
git.ipynb git_tutorial
$ cd git_tutorial
$ pwd
/Users/rafaelpossas/git_tutorial/git_tutorial


Creating a new, empty repository


$ git init
Initialized empty Git repository in /Users/rafaelpossas/git_tutorial/git_tutorial/.git/

Adding your first file to the repository

$ echo > GIT_TUTORIAL.txt "This is my first GIT File"
$ cat GIT_TUTORIAL.txt
This is my first GIT File

Staging area and Status Command

$ git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)


    GIT_TUTORIAL.txt

nothing added to commit but untracked files present (use "git add" to track)

Adding the file to the staging area

$ git add GIT_TUTORIAL.txt
$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

     new file: GIT_TUTORIAL.txt


Commit the file for the first time

$ git commit -m "My first git commit"
[master (root-commit) c7d8e7a] My first git commit
1 file changed, 1 insertion(+)

create mode 100644 GIT_TUTORIAL.txt

Accessing our repository timeline

$ git log
commit c7d8e7ac9608dde2868b1e6bd1eaee222fbeba8b
Author: Rafael Possas <rafael.possas@sydney.edu.au>
Date: Fri May 26 11:22:05 2017 +1000

    My first git commit

Some commands examples for adding files to the staging area


$ git add <list of files> # Add the list of files 
$ git add --all           # Add all files 
$ git add *.txt           # Add all txt files in current directory 
$ git add docs/*.txt      # Add all txt files in docs directory 
$ git add docs/           # Add all files in docs directory 
$ git add "*.txt"         # Add all txt files in the whole project

Git Diff Command

$ echo "Understanding the DIFF command in git" >> GIT_TUTORIAL.txt
$ cat GIT_TUTORIAL.txt
This is my first GIT File
Understanding the DIFF command in git

$ git diff
diff --git a/GIT_TUTORIAL.txt b/GIT_TUTORIAL.txt
index 2f5afae..bfc5e57 100644
--- a/GIT_TUTORIAL.txt
+++ b/GIT_TUTORIAL.txt
@@ -1 +1,2 @@
 This is my first GIT File
+Understanding the DIFF command in git

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
       
        modified: GIT_TUTORIAL.txt

no changes added to commit (use "git add" and/or "git commit -a")

The changes are shown as untracked, in other words, not yet on staging area

$ git add GIT_TUTORIAL.txt
$ git diff

This time it shows no differences, because now the file is already STAGED and ready to commit


$ git diff --staged     # shows the differences for files already in staging area
diff --git a/GIT_TUTORIAL.txt b/GIT_TUTORIAL.txt
index 2f5afae..bfc5e57 100644
--- a/GIT_TUTORIAL.txt
+++ b/GIT_TUTORIAL.txt

@@ -1 +1,2 @@
 This is my first GIT File
+Understanding the DIFF command in git

Git HEAD and Discarding/Unstaging Changes

Image from Software Carpentry (https://swcarpentry.github.io)

$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

     modified: GIT_TUTORIAL.txt


$ git reset HEAD GIT_TUTORIAL.txt
Unstaged changes after reset:
M       GIT_TUTORIAL.txt

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified: GIT_TUTORIAL.txt

no changes added to commit (use "git add" and/or "git commit -a")

The file is no longer in the staging area

What if we wanted to discard all our changes?

$ git checkout -- GIT_TUTORIAL.txt
$ git status
On branch master
nothing to commit, working tree clean

$ git log
commit c7d8e7ac9608dde2868b1e6bd1eaee222fbeba8b
Author: Rafael Possas <rafael.possas@sydney.edu.au>
Date: Fri May 26 11:22:05 2017 +1000

    My first git commit

$ cat GIT_TUTORIAL.txt
This is my first GIT File

$ echo "Understanding the DIFF command in git" >> GIT_TUTORIAL.txt
$ git commit -a -m "Understanding Diff"
[master e58dbad] Understanding Diff
 1 file changed, 1 insertion(+)

$ git log
commit e58dbad79723a437fbcade50bebe4a3ddeb18794
Author: Rafael Possas <rafael.possas@sydney.edu.au>
Date: Fri May 26 11:42:26 2017 +1000

    Understanding Diff

commit c7d8e7ac9608dde2868b1e6bd1eaee222fbeba8b
Author: Rafael Possas <rafael.possas@sydney.edu.au>
Date: Fri May 26 11:22:05 2017 +1000

    My first git commit


$ cat GIT_TUTORIAL.txt
This is my first GIT File
Understanding the DIFF command in git


$ git reset --soft HEAD^


MOVE TO COMMIT BEFORE HEAD (In this case our FIRST commit) 


$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified: GIT_TUTORIAL.txt

$ cat GIT_TUTORIAL.txt
This is my first GIT File
Understanding the DIFF command in git


$ echo "This is my readme file" >> README.txt
$ git add README.txt
$ git commit --amend -m "My first commit & Understanding Diff & Add Readme.txt"
[master 8ac8493] My first commit & Understanding Diff & Add Readme.txt
Date: Fri May 26 11:22:05 2017 +1000
2 files changed, 3 insertions(+)
create mode 100644 GIT_TUTORIAL.txt
create mode 100644 README.txt

$ git log
commit 8ac84931c3671d115cb8297612c7b7eb9b691653
Author: Rafael Possas <rafael.possas@sydney.edu.au>
Date: Fri May 26 11:22:05 2017 +1000

    My first commit & Understanding Diff & Add Readme.txt

Some useful commands

$ git reset --soft HEAD^  #Undo last commit, put changes into staging**
$ git commit --amend -m "New Message" #Change the last commit
$ git reset --hard HEAD^ #Undo last commit and all changes
$ git reset --hard HEAD^^ #Undo last 2 commits and all changes