3. Git 101
Programming Project 2022/23

3.4. Git commands

Now, let's start using git!

Command line interface

Before we choose to use any Desktop client, let us learn how to interact with the command-line interface (CLI).

The git command:

$ git

The git command accepts options:

$ git --version

The git command accepts sub-commands:

$ git log

Many git sub-commands also accept options:

$ git log -p

If you are not sure how to use a sub-command, run:

$ git help add
$ git add --help

Many options have short and long "versions":

$ git commit -m "Fixed a typo."
$ git commit --message="Fixed a typo."

Creating a repository with git init

A Git repository is a database containing all the information needed to retain and manage the revisions and history of a project.

A repository retains a complete copy of the entire project throughout its lifetime.

Let us create a repository.

$ mkdir ~/tmp/myrepo
$ cd ~/tmp/myrepo
$ echo 'My website is alive!' > index.html

To initialize a Git repository, run:

$ git init

git init will create a hidden directory /.git in the current directory. It does not matter if the directory where you ran this command is empty or not.

To see the structure of the created .git directory, run:

$ find .

or

$ tree .git

To install the tree command, you may use:

  • homebrew on macOS
  • apt-get on Linux
  • on Windows, it should already be available

File management: a two-stage process

  1. Stage incremental changes
  2. Commit blocks of change

File states

Checking the state of the repository with git status

To check the current status of the repository, run:

$ git status

This command will show

  • the tracked and untracked files, and
  • what happened to your repository's files, in particular,
    • if they have been added,
    • if they have been modified, or
    • if they have been deleted.

Adding files with git add

To stage a single file, run:

$ git add file.txt

To add multiple files, run:

$ git add file.txt file2.txt file3.txt

To add all files in a directory, run:

$ git add .

Committing changes with git commit

  • A commit is used to record changes to a repository.

  • Committing triggers git to record a snapshot of the sate of the repository.

  • Git does NOT store a copy of all the files in a repository, only those that changed since the last commit.

  • Commits are organized in a chain, with each commit pointing to one or more predecessors.

  • To commit staged changes, run:

    $ git commit

    This command will trigger the shell to open a default editor for you to describe your commit.

  • If you want to add your commit title, use the -m option:

    $ git commit -m "The title of my commit."
  • To also add a commit description, use a second -m:

    $ git commit -m "The title of my commit." -m "The description of my commit."
  • The normal process you will go through is:

    # You create, edit or delete files
    $ echo 'Hello world' > file.txt
    
    # You stage your changes
    $ git add file.txt
    
    # You commit your changes
    $ git commit -m "This message describes the commit."
  • You can stage and commit changes all at once by running:

    $ git commit -a -m "Stage and commit all changes."

    The option -a will automatically commit all changes made to tracked files, including new, modified or deleted files.

Putting all of these commands together

Here is an interaction in which we use all of the aforementioned commands.

  1. We create a directory.
$ mkdir ~/repos/file_state_repo
  1. We initialize a Git repository.
$ cd ~/repos/file_state_repo
$ git init
$ git status
  On branch master

  No commits yet

  nothing to commit (create/copy files and use "git add" to track)
  1. We add two files to the repository. Note how these files are created in the untracked state.
$ echo "New data" > file.txt
$ git status
  On branch master

  No commits yet

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

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

  Create an example file
$ touch data.csv
$ git status
  On branch master

  No commits yet

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

        file.txt
        data.csv
  1. We start tracking one of the files.
$ git add data.csv
$ git status
  On branch master

  No commits yet

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

          new file:   data.csv

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

          file.txt
  1. We commit this newly tracked file.
$ git commit -m "Initial commit"
  [master (root-commit) 1bf2af7] Initial commit
   1 file changed, 1 insertion(+)
   create mode 100644 .gitignore
$ git status
  On branch master
  Untracked files:
    (use "git add <file>..." to include in what will be committed)

          file.txt

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

Configuring the commit author

  1. You can specify the author in every commit:
$ git commit -m "Initial commit" --author "John <john.doe@unibz.it>"
  1. You can set up the author in the project's configuration file:
$ git config user.name "John Doe"
$ git config user.email "john.doe@unibz.it"
  1. You can set it up globally:
$ git config --global user.name "John Doe"
$ git config --global user.email "john.doe@unibz.it"

To see all you current configuration, run:

$ git config --list

Amending your commits

  • Sometimes, we will make a commit and realize that:

    • we forgot to stage some changes
    • the commit message is not clear
  • In such occasions, you can run

    $ git add unstagedFile.txt
    $ git commit --amend
  • If you simply want to change the commit title, run:

    $ git commit --amend -m "My better title"

Removing files with git rm

  • To remove a single file, run:

    $ git rm file.txt
  • To remove multiple files, run:

    $ git rm file.txt file2.txt file3.txt
  • You can also run:

    $ rm file.txt
    $ rm file2.txt
    $ rm file3.text
    $ git add .
  • To remove a file and keep it as unstaged, do

    git rm --cached file.txt

Moving or renaming files with git mv

  • To move or rename a single file, run:

    $ git mv file.txt newFile.txt
  • or

    $ mv file.txt newFile.txt
    $ git rm file.txt
    $ git add newFile.txt
  • or

    $ mv file.txt newFile.txt
    $ git add .

Unstaging changes with git reset

  • To unstage a change to a file, run:

    git reset nope.txt
  • To unstage all staged files, run:

    git reset

When running the last two commands, your changes will not be lost.

Undoing a commit with git reset

  • To undo the last commit and keep its changes unstaged, run:

    $ git reset --mixed HEAD~1
  • To undo the last commit and keep its changes staged, run:

    $ git reset --soft HEAD~1
  • To undo the last commit and throw away the changes, run:

    $ git reset --hard HEAD~1
  • Note that:

    • HEAD refers to the latest commit on the active branch
    • HEAD~1 refers to the second last commit on the active branch

Reversing a commit with git revert

  • Reverting doesn’t alter the existing history within a repository. Instead, it adds a new commit to its history.

  • To revert your last commit, run:

    $ git revert HEAD
  • To revert any commit, run:

    $ git revert 64c852bcb306bceeeec8f77708171c583d807408

Viewing your your repository's history

  • To retrieve the commit history, run:

    $ git log
  • To include the changes introduced in each commit, run:

    $ git log -p
  • For more details on the last commit, run:

    $ git show
  • For more details on a particular commit, run:

    $ git show d6e5980bb9390e853e29b293bc8ecf024b237260
  • For a summary of the commits, run:

    $ git show-branch --more=10w

Visualize Git

  • Let's make things more graphical with Visualize Git:
  • Type help in the command box to see a list of supported operations
    • pres() = Turn on presenter mode
    • undo = Undo the last git command
    • redo = Redo the last undone git command
    • mode = Change mode (local or remote)
    • clear = Clear the history pane and reset the visualization

Supported Git commands:

git branch
git checkout
git cherry_pick
git commit
git fetch
git log
git merge
git pull
git push
git rebase
git reflog
git reset
git rev_parse
git revert
git tag