Git Basic Commands

From Thomas-Krenn-Wiki
Jump to navigation Jump to search

The most important Git commands, which can be helpful for daily routines using this distributed version control system, will be explained in the following article. The article will discuss how to commit changes to the working copy, undo them, compare against other versions or merge them. The prerequisite for working with Git is a basic understanding of the terms used in connection with repositories. They can be found in the Git Basic Terms article or in the Git Community Book. The Git Cheat Sheet also provides a compact list of the most important Git commands.

The examples introduced here have been performed and tested under Ubuntu 11.10. The Git-doc package, which makes a Git tutorial available from the "man gittutorial" command, was also installed on that system.

Configuring User and E-Mail Account Information

Git records the name and email address of the person committing changes for each commit (upload of changes into the repository or repo). Before Git can be used, these values must be set and tested.

git config --global user.name "Test User"
git config --global user.email "tktest@example.com"

This information is globally valid and therefore applies to all repos. The configuration will be subjected to one additional check.

:~$ git config --global -l
user.name=Test User
user.email=tktest@example.com

The ignore files setting is one more useful setting to set before creating a repo in a directory. Files that should not be subject to version control are listed in the ".gitignore" file. This setting makes sense for directories that contain compiled executables (for example, the "bin" directory).

echo bin >> .gitignore

Basic Routines for Working with a Git Repo

Assume that there are files already in the "project" directory, whose version should be controlled. To accomplish this, Git will be called from this directory.

:~/Repos/project$ git init
Initialized empty Git repository in /home/tktest/Repos/project/.git/

At this point, no files are subject to version control. This will be changed for the existing "main.c" file, as follows:

:~/Repos/project$ git add main.c
:~/Repos/project$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#	new file:   main.c
#
:~/Repos/project$ git commit -m "new main file created"
[master (root-commit) ce004cb] new main file created
 1 files changed, 6 insertions(+), 0 deletions(-)
 create mode 100644 main.c

The "main.c" file has been added to the repo by means of the commit command. When this file is edited, the changes will be logged and can be edited (commit, revert etc.). For testing purposes, a new line can be added and checked by means of "git diff".

:~/Repos/project$ git diff main.c
diff --git a/main.c b/main.c
index b424dc0..678e613 100644
--- a/main.c
+++ b/main.c
@@ -2,5 +2,6 @@
 
 int main(void){
        printf("Hello World...\n");
+       int i = 42;
        return 0;
 }

Furthermore, the status indicates that a file has been edited.

:~/Repos/project$ git status
# On branch master
# 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:   main.c
#
no changes added to commit (use "git add" and/or "git commit -a")

This line will also be added to the repo.

:~/Repos/project$ git commit -a -m "Line with integer value"
[master 58d507f] Line with integer value
 1 files changed, 1 insertions(+), 0 deletions(-)

The Git log will also have already recorded the two commit commands.

:~/Repos/project$ git log
commit 58d507f3b1ede925708bce7b6e443da296285f3c
Author: Test User <tktest@example.com>
Date:   Wed Jan 18 10:36:54 2012 +0100

    Line with integer value

commit ce004cbff963c582e381c074f64790dd908db76b
Author: Test User <tktest@example.com>
Date:   Wed Jan 18 10:28:57 2012 +0100

    Created new main file

Next, a simple function will be created.

:~/Repos/project$ git diff main.c
diff --git a/main.c b/main.c
index 678e613..fa55f09 100644
--- a/main.c
+++ b/main.c
@@ -1,7 +1,12 @@
 #include <stdio.h>
 
+void help(void){
+       printf("This is a simple help message...\n");
+}
+
 int main(void){
        printf("Hello World...\n");
        int i = 42;
+       help();
        return 0;
 }

This change can be rejected as follows:

:~/Repos/project$ git checkout -- main.c
:~/Repos/project$ git diff main.c

If changes that have been added to the repo need to be undone, this can be accomplished using a Revert command. This does not represent a problem for the most recent commit. With previous commits that may potentially overlap, conflicts will have to be edited manually (cp. a merge process). Thus, the last commit has been undone and will automatically lead to a new commit.

:~/Repos/project$ git revert HEAD 
[master 1f8bc85] Revert "Added commet to print"
 1 files changed, 0 insertions(+), 1 deletions(-)

The Git Show command will check the last entry in the repo.

:~/Repos/project$ git show
commit 1f8bc856da19c40b9f3eb01e4415c870a19afcd8
Author: Test User <tktest@example.com>
Date:   Wed Jan 18 11:30:40 2012 +0100

    Revert "Added commet to print"
    
    This reverts commit 63d4297d98b1c0eaddde11b902268fa4e4de9c3c.

diff --git a/main.c b/main.c
index 4b4121d..678e613 100644
--- a/main.c
+++ b/main.c
@@ -1,7 +1,6 @@
 #include <stdio.h>
 
 int main(void){
-       //print out hello world
        printf("Hello World...\n");
        int i = 42;
        return 0;

Git Branches

A branch is a custom development path, which is used, for example, for developing and testing a new feature before first including it into the master branch once the components have been completed. Changes made by other programmers, which should be checked first, will also be ideally processed in a separate branch.[1] A branch will be created in the following example for adding a new function to the file, "main.c". First, all of the existing branches will be listed.

:~/Repos/project$ git branch
* master

The Master branch is the default branch, which was automatically created during the creation of the repo. The new "Function" branch will now be added.

:~/Repos/project$ git branch function
:~/Repos/project$ git branch 
  function
* master
:~/Repos/project$ git checkout function
Switched to branch 'function'

A new function will be added and tested in the Function branch. Finally, the Function branch will be merged back into the Master branch.

:~/Repos/project$ git status
# On branch function
# 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:   main.c
#
:~/Repos/project$ git commit -a -m "added help function"
[function c1b6fa4] added help function
 1 files changed, 7 insertions(+), 0 deletions(-)
:~/Repos/project$ git checkout master
Switched to branch 'master'
:~/Repos/project$ git status
# On branch master
nothing to commit (working directory clean)
:~/Repos/project$ cat main.c 
#include <stdio.h>

int main(void){
	printf("Hello World...\n");
	int i = 42;
	return 0;
}

The changes from the Function branch will be added back into the Master branch.

:~/Repos/project$ git merge function
Updating 1f8bc85..c1b6fa4
Fast-forward
 main.c |    7 +++++++
 1 files changed, 7 insertions(+), 0 deletions(-)

The Git Show command shows the most recent changes, in turn.

:~/Repos/project$ git show main.c
commit c1b6fa4a907f4413ea18ad5f3bdc6a1717fe1371
Author: Test User <tktest@example.com>
Date:   Wed Jan 18 12:56:20 2012 +0100

    added help function

diff --git a/main.c b/main.c
index 678e613..7bc02a2 100644
--- a/main.c
+++ b/main.c
@@ -1,7 +1,14 @@
 #include <stdio.h>
 
+void help(void){
+       printf("a simple help message\n");
+       return 0;
+}
+
+
 int main(void){
        printf("Hello World...\n");
        int i = 42;
+       help();
        return 0;
 }

References

Related articles

Git-annex Archive with Git-annex Assistant
Git-annex detailed information
Git-annex Repository on an External Hard Drive