skip navigation

www.Hilands.com


Content:: Git - Handling repositories

Git - Handling repositories
Create a new repository

Table of Contents
Initialize repository and adding a file
Make a directory that your repository will live. $ mkdir testrepo1
$ cd testrepo1/
Initialize the repository in your current directory. $ git init
Initialized empty Git repository in /home/pallen/code/c/testrepo1/.git/


Edit your description $ nano .git/description
Test repository; learning how to use git!
$ git commit -m "initial commit"
$ nano hello_world.c
#include <stdio.h>
int main (void)
{
	printf("Hello World!\n");
}
We can now use git status and git will detect that a new file exists in the directory but is not currently tracked. $ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       hello_world.c
nothing added to commit but untracked files present (use "git add" to track)


Add our file to the repository $ git add hello_world.c
Alternatively we could use $ git add .
to add all files.
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   hello_world.c
#
$ git commit -m "adding hello_world.c"
[master (root-commit) 6b29ef8] adding hello_world.c
 1 files changed, 5 insertions(+), 0 deletions(-)
 create mode 100644 hello_world.c


We can now run see information in the git log. $ git log
ESC[33mcommit 6b29ef809d5362b80a03e6abf5fd9264135665a5ESC[m
Author: Philip Allen <phil@hilands.com>
Date:   Fri Sep 6 16:13:35 2013 -0700

    adding hello_world.c
Modify files in the repository
Lets make some simple changes to our hello_world.c file $ nano hello_world.c We'll add some comments to the top of the file on how to compile the program.
/*
# gcc helloworld.c -o helloworld
# ./helloworld
*/
Check the status of the file $ git status
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   hello_world.c
#
no changes added to commit (use "git add" and/or "git commit -a")
If we try to commit we will get a notice stating that our files have been modified and there are no changes at this time. Similar to the status message above. $ git commit -m "added compiling comments to hello_world.c source"
 hello_world.c source"
# On branch master
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   hello_world.c
#
no changes added to commit (use "git add" and/or "git commit -a")
To commit the changes we'll have to tell git to add the modified file $ git add hello_world.c
Run git status to see the changes to be committed. $ git status
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   hello_world.c
#
$ git commit -m "added compiling comments to hello_world.c source"
[master c81a803] added compiling comments to hello_world.c source
 1 files changed, 6 insertions(+), 0 deletions(-)
Alternatively we could use the -a switch while performing the commit to automatically add the changes. $ git commit -a -m "added compiling comments to hello_world.c source"
Running git status will show a clean working directory again. $ git status
# On branch master
nothing to commit (working directory clean)
References