Git Basics

Introduction

Contents

This page contains a quick reference of the basic Git commands to add, delete, commit and compare files in Git. It also shows how to create a Git repository easily. This is part 2 in the Git quick reference series:

Create a new Repository

There are two types of Git repositories: development and bare repositories.

A development repository is used for daily development work. It has a .git directory and a checked-out working copy of the repository. The .git repository contains the local version of the Git repository. Developers use the working copy to add, update and remove files.

A bare repository only contains the .git directory, and cannot be used for development. Developers use bare repositories to publish and share their work with other developers.

Create a new Repository

or

Create a new Repository from an existing Directory

Create a Bare Repository

Clone a Repository

The below git commands will clone a Git repository into the local directory.

The below command specifies an alternative name for the local repository

Clone a Bare Repository

Get Working Area Status

Working area without changes

Working area with changes

Add Files

Adding a specific file

Adding everything under a Directory

When git add  is used on a directory name, all files and subdirectories are staged recursively.

Commit Files

Commit Staged Files

Commit All Modified Files (Not only Staged)

By default, only staged files are committed. You need to use the -a  or --all  flags to commit all staged and unstaged files.

Amend the last commit message

The --amend  flag changes the message used for the last commit. However, be careful amending history.

Reverting Files

Hard delete commits / Re-point branch head

The git reset --hard  statement re-points the current branch head to a specific tag or commit. The danger of this command is that it gets rid of the latest commits, thus re-writing history.  It is useful if you haven’t published your changes yet. It might, however, make you a very unpopular developer if you have published and another developer picked up your changes.

This command resets the current branch head to a specific commit:

This command reverts the latest three commits (HEAD, HEAD~1 and HEAD~2)

Revert a commit

The git revert  command reverts an existing commit by applying a reverse patch . The reverse patch is then committed to git.  The benefit of git revert  is that it maintains the version control history. On the other hand, git reset --hard  is dangerous because it re-writes history. git revert  is the preferred command to revert a change that has already been published.

Reverting Staged Files

The git reset HEAD command tells Git to revert a file in the staging area to the latest repository version. The version in the staging area is moved to the “not staged for commit” area.

The --  command is useful to tell Git that all command arguments are done, and the remaining parameters in the command line are files or directory names. This is useful when files clash with branch or tag names.

Reverting Unstaged Changes

You can revert all unstaged files from a directory

or for a specific file

Remove Files

Removing a File

Removing a Directory

Recover a Directory Staged for Removal

Pull and Push changes

Fetching Changes from a Remote Repository

Pulling Changes from a Remote Repository

Pushing Changes to Remote Repository

Ignore Files

Git can ignore temporary and unwanted files using the .gitignore file.  The exclusions in a .gitignore file at the top level of the working directory affects the whole repository. A .gitignore file in a subdirectory will only apply to that directory and sub-directories.

The following .gitignore file excludes a random.txt file, and all files with tmp and class extension.

View Commit History

Default Log

By default, git log  with no arguments shows all commits made in the repository in reverse chronological order. It shows each commit with its SHA-1, author and commit message.

Pretty Logging

With the --oneline  option, git log will show the SHA-1 and message of each commit in one line.

Alternatively, you can use the --pretty:format  to specify the exact formatting

You can also use the format.pretty  config variable to change the default git log  format:

Show Last N Commits

Show Commits Excluding Last N Commits

Show Commits between Two Dates

Limit the Output to a File or Directory

Filter Commits by Commit Message

Filter Commits by Author

Filter Commits By Code Changed

Show the Patch in Each Commit

Show Commit Stats

Show Branch and Merge History

The --graph  flag shows a text-based graph representation of the commit history and merges. The --all  flag shows branches. The --decorate  flag shows the ref name of each branch.

Show Differences

The git diff  tool compares two commits, or the working area against a commit.

It should be noted git diff  differs from git log  in that git diff  doesn’t care about the history of the files. Git diff just compares two branches or two points in time, regardless of history. On the other hand, git log  is very conscious about the commit history.

Differences between Working Tree and Staging Area

Differences between Staging Area and Repository

Differences between Working Tree and Repository

Differences between Two Commits

Statistics about Changes

Differences only in a Specified Path

Reference

  • Git Reference: http://git-scm.com/doc
  • Pro Git. Scott Chanon and Ben Straub. APress. http://git-scm.com/book/en/v2
  • Pragmatic Guide to Git. Travis Swicegood. Pragmatic Bookshelf, 2010.
  • Version Control with Git. Jon Loeliger and Mattew McCullough. O’Reilly, 2012

Git Tools

The following two tabs change content below.

Eduard Manas

Eduard is a senior IT consultant with over 15 years in the financial sector. He is an experienced developer in Java, C#, Python, Wordpress, Tibco EMS/RV, Oracle, Sybase and MySQL.Outside of work, he likes spending time with family, friends, and watching football.

Latest posts by Eduard Manas (see all)

Leave a Reply