A quick example shows how to start a new project with Git.

Initial Configuration

The first time that you use git, you should register your preferred name and email; you only have to do this once - it will be recorded by git in:


In case you prefer emacs over vim then also configure it as the preferred editor:

$ git config --global user.name "Firstname Lastname"
$ git config --global user.email This email address is being protected from spambots. You need JavaScript enabled to view it.
$ git config --global core.editor emacs

Start a new Project

Here we assume you are working on the build host, and that you are going to add a new small test project to the test repository.

You will init a local git project, add content to it, commit it, add the remote test repository and push it up to the remote.

First you must initialize git in your local working directory.  It does not matter if you already have files there.  This example has a directory with some files in it that perform a user shutdown on some legacy unix servers.

$ cd ~/newproject/
$ ls -R # see files under this project
COPYING  Makefile  shut.tk*  userShutdown.c  userShutdown.c.orig

Initialize your local git working area:

$ git init

Initialized empty Git repository in $HOME/testproject/.git/

Now add all the files at once to your local git:

$ git add *

Check the status at any time:

$ git status
# On branch master
# Initial commit
#   (use "git rm --cached ..." to unstage)
#       new file:   shut-1.0/COPYING
#       new file:   shut-1.0/Makefile
#       new file:   shut-1.0/shut.tk
#       new file:   shut-1.0/userShutdown.c
#       new file:   shut-1.0/userShutdown.c.orig

Commit these files locally:

$ git commit -a

This invokes the editor, where you provide a comment.  It always presents some informative help about what you are commiting. 

Here the comment I add is highlighted as italics:

- This is my test project
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Initial commit
# Changes to be committed:
#   (use "git rm --cached ..." to unstage)
#       new file:   shut-1.0/COPYING

Check the status again:

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

Add a remote so that we can push local repository to there, here 'origin' is just a name for the remote.

Note that you could have done this right away after issuing 'git init' - it doesn't matter when.

$ git remote add origin ssh://This email address is being protected from spambots. You need JavaScript enabled to view it./data/test.git
Because the remote repository already has some content, we first need to issue a pull to get that content.  This will pull down everything in that repository to our local directory, 'merging' it with our own content.  Obviously if there was a conflict of some kind then we would have to resolve it.  

This pull would not be necessary if the remote was a brand new, empty repository

$ git pull origin master
From ssh://git.fibrevillage.com/data/test
 * branch            master     -> FETCH_HEAD
Merge made by the 'recursive' strategy.
 container/html/index.html             |   9          
 container/images/fedora-logo-icon.png | Bin 0 -> 10857 bytes
 2 files changed, 9 insertions( )
 create mode 100644 container/html/index.html
 create mode 100644 container/images/fedora-logo-icon.png

ls local files

$ ls
container/  shut-1.0/

Now I can push it to the default HEAD branch, 'master':

$ git push origin master
Counting objects: 11, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (9/9), done.
Writing objects: 100% (10/10), 5.10 KiB | 0 bytes/s, done.
Total 10 (delta 1), reused 0 (delta 0)
To ssh://This email address is being protected from spambots. You need JavaScript enabled to view it./data/test.git
   9ed82fa..17df0ee  master -> master

That is it. 

Note that instead you could have just cloned the remote repository, and then added content to it with git add/commit/push as well.  It all depends on what you are trying to accomplish.


Comments powered by CComment