Git

a distributed version control system

What is version control

Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later.

Distributed Version Control Systems

In a DVCS, clients don’t just check out the latest snapshot of the files: they fully mirror the repository.

Down arrow

Git Basics

Every time you commit, Git takes a picture of what all your files look like at that moment and stores a reference to that snapshot.

Down arrow

To be efficient, if files have not changed, Git doesn’t store the file again—just a link to the previous identical file it has already stored.

Nearly Every Operation Is Local

Most operations in Git only need local files and resources to operate.

For example, to browse the history of the project, Git doesn’t need to go out to the server to get the history and display it for you—it simply reads it directly from your local database.

check-summ

Everything in Git is check-summed before it is stored and is then referred to by that checksum. This means it’s impossible to change the contents of any file or directory without Git knowing about it.

Down arrow

The mechanism that Git uses for this checksumming is called a SHA-1 hash. This is a 40-character string composed of hexadecimal characters (0–9 and a–f) and calculated based on the contents of a file or directory. A SHA-1 hash looks something like this:

24b9da6552252987aa493b52f8696cd6d3b00373

The Three States

  • committed
  • modified
  • staged

Git workflow

  1. You modify files in your working directory.
  2. You stage the files, adding snapshots of them to your staging area.
  3. You do a commit, which takes the files as they are in the staging area and stores that snapshot permanently to your Git directory.

Git Basics

Now we will get for basic command.

Getting a Git Repository

You can get a Git project using two main approaches. The first takes an existing project or directory and imports it into Git. The second clones an existing Git repository from another server.


Down arrow

Initializing a Repository in an Existing Directory


							$ git init
						

Cloning an Existing Repository

git clone [url]


							$ git clone https://github.com/Cotard/git-presentation.git
						

Files lifecycle

Checking the Status of Your Files


						$ git status
# On branch master nothing to commit (working directory clean)
					

Checking the Status of Your Files

Let’s say you add a new file to your project, a simple README file. If the file didn’t exist before, and you run git status, you see your untracked file like so:


$ vim README
$ git status
# On branch master
# Untracked files:
#   (use "git add ..." to include in what will be committed)
#
#   README
nothing added to commit but untracked files present (use "git add" to track)
					

Tracking New Files


$ git add README
					

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

Ignoring Files

Here is example .gitignore file:


# a comment - this is ignored
# no .a files
*.directory

# only ignore the root TODO file, not subdir/TODO
/TODO
					

Viewing Your Changes


$ git diff
diff --git a/benchmarks.rb b/benchmarks.rb
index 3cb747f..da65585 100644
--- a/benchmarks.rb
+++ b/benchmarks.rb
@@ -36,6 +36,10 @@ def main
           @commit.parents[0].parents[0].parents[0]
         end

+        run_code(x, 'commits 1') do
+          git.commits.size
+        end
+
         run_code(x, 'commits 2') do
           log = git.commits('master', 15)
           log.size


					

Committing Your Changes

Your staging area is set up the way you want it, you can commit your changes.


$ git commit
					

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Committer: user 
#
# On branch gh-pages
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
#       modified:   index.html
#
					

To not to write message in editor, I use command:


						$ git commit -a -m "commit message"
					

Links

THE END