Git – Basic commands

Git – Basic commands

software_development
English text only

This article describes some useful Git commands that every developer should know.

Configuration commands

Before using Git, it’s usually best to set up its environment using the following commands:

# set name and email
git config --global user.name "John Doe"
git config --global user.email "john.doe@example.com"

 # set color on your cli
git config --global color.ui "auto"

# set command alias "ci" for "commit"
git config --global alias.ci "commit"

# show configuration
git config --global --list

Starting a repository

Git is a distributed version control system. Instead of having one central repository that you send changes to, your own local repository contains the entire history of the project.
You can also send your changes to an upstream repository (see gitolite), share it with other developers, and fetch changes from it.

# initialize a local repository
git init
# clone a local repository
git clone </some/existing/repo> </some/new/location>
# clone a remote repository
git clone  git@<myRepoServer>:</location/myrepo>[.git] [</my/location>]

Working with Git

Your code in Git can be stored in three different places:

  • working tree
  • index or staging area
  • repository

To understand how Git works, try these steps:

# make a temp dir
mkdir myRepo
cd myRepo

# initialize a git repo
git init

# create a file using your favorite editor
touch myFile.txt

# add it to git repo
git add myFile.txt

# commit
git commit -m "add in myFile.txt"

# make a change to myFile.txt and add it to the staging area
echo "add1" > myFile.txt
git add myFile.txt

# see git status
git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   myFile.txt
#

# make another change to  myFile.txt
echo "add2" > myFile.txt

# see git status
git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   myFile.txt
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   myFile.txt
#

The diff command will show the differences between the three places:

# look for changes in your working tree that you haven't staged or committed yet
git diff

diff --git a/myFile.txt b/myFile.txt
index dd662fa..6011347 100644
--- a/myFile.txt
+++ b/myFile.txt
@@ -1 +1,2 @@
 add1
+add2
# look for changes between the staging area and the repository
git diff --cached

diff --git a/myFile.txt b/myFile.txt
index e69de29..dd662fa 100644
--- a/myFile.txt
+++ b/myFile.txt
@@ -0,0 +1 @@
+add1
# look for changes between repository and working tree
git diff HEAD

diff --git a/myFile.txt b/myFile.txt
index e69de29..6011347 100644
--- a/myFile.txt
+++ b/myFile.txt
@@ -0,0 +1,2 @@
+add1
+add2

Commit

Here are the basic commands to handle local commits:

# add a new file or stage an existing file
git add <myFile>
# commit staged changes
git commit -m "<commit message>"
# undo a staged commit (the staging area is reset but your
# changes in the working tree are preserved)
git reset HEAD

Undoing Changes

# remove changes in you file in the working tree
git checkout HEAD <myFile>
# undo a staged commit and reset the working tree changes
# to the HEAD (untracked files are preserved)
git reset --hard HEAD

Ignoring files and directories

To ignore some files or directories, add and edit .gitignore file in the root folder of your repository.

If your files have to be ignored only locally, edit .git/info/exclude file.

Here are some ignore syntax rules:

# put these on .gitignore or in .git/info/exclude
myFile # matches myFile in all the working tree
/myFile # matches myFile only in the root path of the working tree
myDir/myFile # matches myFile in myDir starting from the root of the working tree
myDir/fooDir # matches the sub directory fooDir with all its contents
ba* # you can use GLOB patterns
!bar # a trailing ! negates the pattern

Glob Patterns

* – matches zero or more of any character

? – matches any one character

[list] – matches any one character in the list of characters;
the list can contain a range of characters such as [a-zA-Z]
and can be negated/complemented by using ^ or ! at the start,
e.g. [^a-zA-Z] = not a letter; [!0-9] = not a digit

Log

# see all the commit log
git log
# see the last n lines of the commit log
git log -n

Help

# help for a git command
git help <command>

See you soon with a quick reference about working with branches in Git…

Links

http://git-scm.com
https://github.com
http://sitaramc.github.com/gitolite

Related News