Introduction to Git

Git is a version control software designed by Linus Torvalds, designed for efficiency and reliability of maintaining applications versions when they have many source-code files. Git purpose is to keep track of file changes and coordinate the work of several people on shared files.

  • Git stores changes in a special database called a repository.
  • Version Control allows you to track change history and work with a team.

Version Control System

A Version Control System is a system that manages the various changes that are made to the elements of a product or its configuration. There are two types of version control systems:

  • Centralized
  • Distributed

Centralized

In a centralized system, all the team must connect to the server to get the latest version of the code, and to share their changes (Ex. Subversion, Team Foundation Server).

Centralized System

Distributed

In a distributed system, each member has a copy of the project, allowing snapshots to be saved locally; it also allows you to synchronize your work with other contributors (Ex. Git, Mercurial).

Distributed Systems

Advantages of Using Git

  • It’s free
  • Open Source
  • Fast
  • Scalable
  • Allows branching/merging

Configuration Levels

Git allows us to configure it at different levels:

  • System: All users
  • Global: All repositories of the current user
  • Local: The current repository
> git config --[level] [setting] "[name]" # Command structure
> git config --global user.name "Jhon Doe" # Set username
> git config --global user.email jhondoe@gmail.com # Set user email
> git config --global core.editor "code --wait" # Set default editor

End of Lines

Its important to configure the end of line setting when working with a team that uses different Operating Systems, this is due to the way different Operating Systems manage end of lines. On Windows end of liens are marked with two characters \r and \n but on other operating systems such as MacOS and Linux end of lines are marked only with one character \r. To fix this we need to configure a setting called core.autocrlf. This setting will tell Git to manage end of line depending on which OS we are using.

> git config --global core.autocrlf input # MacOS/Linux
> git config --global core.autocrlf true # Windows

The Basics

Workflow

When we are working in a project we usually update, create, and delete files. We keep doing this action until we have a version of the project that we are happy with. Once we are happy with our project, we must commit the changes so Git can manage our files. But before we commit changes, we need to include them in the Staging Area. This area stores the files that we want to include in the next snapshot stored by git.

Git Workflow

Staging Area

The Staging Area is a space where we can add and organize the files that will be included in the next commit (in other words, the next version of the project).

> git add [filename] # Add a single file to the Staging Area
> git add . # Add all files recursively
> git add *.txt # Add files using patterns

If needed files can be removed from the staging area.

> git reset HEAD -- path/to/file # The specified file will be removed from the staging area

Git Commit

The commit command records the changes on the repository that are in the staging area. Each commit stores the following information:

  • ID
  • Message
  • Date/Time
  • Author
  • Complete Snapshot
> git commit -m "[message]" # Commits with given message
> git commit # Opens the editor and allow to put descriptions in the commit

Git compresses the content and doesn’t store duplicate content.

Skip Staging Area Sometimes we need to skip the Staging Area, this is possible but risky. It is highly discouraged and should only be used when the code is to be reviewed.

> git commit -a -m "[message]" # Add all files to the Staging Area and commits them
> git commit -am "[message]" # Shorter version

You can also revert a commit if needed; this can be done in three different forms, these are: --hard, --soft and --mixed.

> git reset --soft HEAD~1 # Last commit will be removed
> git reset --hard HEAD~1 # Last commit will be removed, and changes will be reverted
> git reset --mixed HEAD~1 # Last commit will be removed, and staging area will be reset

Removing Files

If we need to remove files, we can use the following commands:

> git ls-files # Show information about files and the working tree
> git rm [file] # Removes a file from the working tree
> git rm [file1] [file2] # Remove multiple files from the working tree
> git rm *.txt # Remove files using patterns

Renaming or Moving Files

Renaming or moving files is a two-step operation:

> git add old_file.txt # Add original file
> git add new_file.txt # Add renamed file

Although this solution works, git provides a special command to rename/move files.

> git mv -f "[old name]" "[new name]" # Renaming/moving folders
> git mv [old file] [new file] # Renaming/moving files

Ignoring Files

In almost all projects we need to ignore certain files or directories that are unnecessary for the operations of the projects (e.g. logs, binary files etc.).

The .gitignore file specifies intentionally untracked files that Git should ignore. Files that are been tracked already will not be affected.

Remove a File We can remove files from the working directory using the rm command.

> git rm -h # Help on rm command
> git rm [file] # Remove file from working directory and staging area
> git rm --cached [file] # Remove file from staging area

Showing Working Directory and Staging Area

git status shows the current state of your working directory and staging area.

> git status # Shows the state of the working directory
> git status -s # Shows the state of the working directory in a concise manner

Viewing the Staged and Unstaged Changes

The git diff command shows the changes between commits, commit and working tree etc.

> git diff # Shows unstaged changes
> git diff --staged # Shows changes between the stage and commits

Viewing the Logs

The git log command shows the commit logs. Each commit has a unique identifier, this identifier is a 40-character hexadecimal string.

> git log # Shows the commit log
> git log --oneline # Shows the commit log in one line
> git log --reverse # Shows the commit log reversed

Viewing a Commit

The show command give us expanded details on the Git objects, such as blobs, trees, commits, tags, etc.

blobs == files

trees == directories

> git show
> git show [commit ID] # Show a specific commit
> git show HEAD # Show last commit
> git show HEAD~1 # Show second to last commit