Git

Concepts

Terminology

  • Repository (repo)
  • Working Directory/Tree
  • Staging Area / Index
  • Commit
  • Hash (SHA-1)
  • Clone
    • upstream
    • origin
  • Branch
    • main (master)
  • HEAD
  • Check in / Check out
  • Merge
  • Local/Remote repository
  • Push / Pull
  • Pull Request

A Git repository

A git repo with working directory

A Git repo

A git repo

Multiple repositories

Multiple repos

With remote repository

Remote repo

A commit

A commit is a snapshot of contents in working directory (prepared in staging area).

Making a commit

A commit ID

  • A commit is uniguely identified by an ID

    • commit ID: a 40-hex-digit SHA1 hash.
  • Any commit can be referred to by

    • full SHA-1 => 40-hex digit
    • short SHA-1 => first few hex digits that can uniuely identify the commit
    • tag => an alias given to a commit
  • An example of a commit from running git log:

commit 84d06cdc06389ae7c462434cb7b1db0980f63860 (HEAD -> master,   origin/master, origin/HEAD)
Merge: 26c4f98f 48bf2fa8
Author: Junio C Hamano <gitster@pobox.com>
Date:   Fri Mar 26 14:59:47 2021 -0700

A commit graph

Commit graph

Git's commit graph

Git's commit graph

Git installation

Command-line tools

GUI Clients

Git with Editor/IDE

  • Visual Studio Code

Git via Git

If git is already installed:

git clone https://github.com/git/git

Configuring Git

Configuration levels

There are 4 levels of configurations

  1. system-side
  2. per user
  3. per repository
  4. per worktree

Use option to specify level of a configuration

git config --system   # require admin permission
git config --global
git config --local
git config --worktree
git config            # default to --local

Getting Help

Get help on command-line option summary:

git command -h

Get manual page help on:

git command --help

Configure username and email

It's is conventional to supply your name and email.

  • required to start work with git
  • used as reference when working in a team
git config --global user.name "Name Surname"
git config --global user.email "user@ssome_domain"

Edit configuration file

Configuration file format is similar to .ini format.

git config --system -e  # require admin permission
git config --global -e
git config --local -e
git config -e           # default to --local

Configure default editor

Configure editor to use when writing commit message.

Examples of some editors

git config --global core.editor "code --wait"
git config --global core.editor "nano -w"
git config --global core.editor "vim"

Default depends on systems and installation.

Configure end of line 1

End of line:

  • on Windows (CR LF) - Carriage Return, Line Feed
  • on Linux/MacOS (LF) - Line Feed

Should store end of line as LF in repository.

For Linux/MacOS:

git config --global core.autocrlf input

For Windows:

git config --global core.autocrlf true

Configure end of line 2

Add file .gitattributes to your repository with content.

# Contents in .gitattributes
* text=auto

Create a repository

Create a new repository

Create a repository inside a directory:

mkdir myproject
cd myproject
git init

Or create a new directory with a repository:

git init myproject

Create a cloned repository

Clone a local directory:

git clone /path/to/local/directory [optional new directory name]
git clone /d/projectA [optional directory name]

Clone a remote repository

git clone url

Work locally

Linear history workflow.

  • create/clone a repository
  • create/modify files/directories
  • add changes to staging area
  • review changes
  • commit changes

Example initial setup

git init myproject

Stage files

Add some files to working directory.

Then add these changes to staging area

git add f1.txt          # add single file
git add f1.txt f2.txt   # add multiple files
git add *               # add all files in current directory (shell expands the asterisk)
git add .               # add all files recursively from this directory

Check status

Check changes between

  • staging area and HEAD (HEAD usually points to master/main)
  • working directory and staging area
  • untrackd files
git status     # long description
git status -s  # short status using characters

Commit changes

Make a commit with commit message.

git commit -m "Initial commit"

If run without -m option, git will open default editor.

git commit

Common operations

Checkout a branch

Updates files in the working tree to match given branch or current branch.

git checkout main        # update working tree to branch main
git checkout master      # update working tree to branch master
git checkout feature_x   # update working tree to branch feature_x

# since git version 2.23
git switch main          # update working tree to branch main
git switch master        # update working tree to branch master
git switch feature_x     # update working tree to branch feature_x

Undo changes in working directory

Make contents in working directory the same as in staging area.

git restore f1.txt        # restore single file
git restore f1.txt f2.txt # restore multiple files
git restore *             # restore all files in current directory (shell expands the asterisk)
git restore .             # restore all files recursively from this directory

Undo changes in staging area

Making contents in staging area the same as in HEAD. Also called "unstage changes".

git restore --staged f1.txt        # restore single file
git restore --staged f1.txt f2.txt # restore multiple files
git restore --staged *             # restore all files in current directory (shell expands the asterisk)
git restore --staged .             # restore all files recursively from this directory

Restore a file to some previous version

git restore -s commit_ID f1.txt        # restore f1.txt to version in commit_ID
git restore --source commit_ID f1.txt  # the same with long option

See the history

git log            # show log from the latest commit
git log --reverse  # show log from the first commit
git log --oneline  # show log of one line for each commit
git log --pretty=oneline  # show pretty format in one line

See difference between trees

See changes in working directory from staging area

git diff

See changes in staging area from HEAD

git diff --cached
git diff --staged  # synonymous to --cached

Use VS Code as visual diff tool

Configure diff.tool with a name "vscode":

git config --global diff.tool vscode

Configure difftool with the actual command:

git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE'

See changes with diff tool.

git difftool
git difftool --staged

See a commit details

git show commit_ID

Remove files

Remove files that are already tracked by Git.

git rm f1.txt
git rm f1.txt f2.txt
git rm *

Rename files

git mv f1.txt f3.txt

Ignore files

Add file .gitignore to your repository

Example of contents in .gitignore

# Object files  "#" starts line comment
*.o             # ignore all files that end with ".o"
*.obj           # ignore all files that end with ".obj"

Create a tag

Create a tag that will be associated with a commit.

git tag v1          # tag current commit with name "v1"
git tag v2 daf723e  # tag commit id "daf723e" with name "v2"

Create custom short command with alias

Create some common operations with shorter keystrokes.

git config --global alias.st status
git config --global alias.ci commit
git config --global alias.la "log --all --oneline --decorate"
git config --global alias.hist "log --pretty=format:'%C(auto)%h %Cgreen(%cd)%C(auto)%d %C(auto)%s %C(cyan)[%an]' --graph --date=short"

References