A quick example shows how to start an existing project with Git.

Initial Configuration

This step only need to do once, skip this step if you have git environment setup done.

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:

 $HOME/.gitconfig

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

Note: compare to CVS, the colone subcommand is equivelant to cvs checkout, the different is that Git receives a full copy of nearly all data that the server has, another word, Every version of every file for the history of the project is pulled down by default when run git clone.

Cloning an existing Git Project

Want to get a copy of an existing Git repositiry, use the command

git clone

For example, to clone the Git linkable library called libgit2, do so

git clone https://github.co/libgit2/libgit2

The command creates a diretory named libgit2, initializes a .git directory inside it, pulls down all the data for that repository, and checks out a working copy of the latest version. To check files in the project, just go into the directory and see

$ cd ~/oldproject/
$ ls -R # see files under this project

Initialize your local git working area:

$ git init

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

To add new files 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