Sunday, February 13, 2011

Git tutorial -- Getting started with git (hosting and cloning)

I have used git a bit in classes and for github projects created by others. But today I'm taking the initiative to learn it from scratch. My friend Phil set up a Ubuntu virtual machine for me on his server, where I plan to do a very small web project with dcluo. Git was not installed.

Creating a git repository on a directory for a project:

0. My directory where I want to make into a git repository is /var/www/game1. There is currently only 1 file inside it: index.html

1.* Install git from Ubuntu terminal: sudo apt-get install git-core git-doc
* See note below *

2. Create git repository: git init
Initialized empty Git repository in /var/www/game1/.git/

3. Check it's working: git status
# On branch master
#
# Initial commit
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# index.html
nothing added to commit but untracked files present (use "git add" to track)

4. git config --global user.name "Your Name"
5. git config --global user.email youremail@email.com

6. git add index.html and add other files similarly

7. git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: index.html
#

8. git commit -a -m "brand new start" the "-a" adds all the changed files (yes, it's redundant here because I have added my only file in step 6, but it's good to know)
[master (root-commit) d38fbfd] brand new start
1 files changed, 22 insertions(+), 0 deletions(-)
create mode 100644 index.html

9. git status
# On branch master
nothing to commit (working directory clean)

10. git log note it doesn't say "commit #1", it's good because ordering by numbers would be confusing if you merge or revert commits in the future
commit d38fbfd10003a28859d5b943b729084bfcf6b78d
Author: HaoQi Li <youremail@email.com>
Date: Sun Feb 13 06:14:54 2011 -0500

brand new start

Getting that git repository directory on another machine:

11. On another machine, make a directory where you want to clone the repository. mkdir anotherDir

12. cd anotherDir

13. git clone haoqi@haoqi_s.host.com:/var/www/game1 . don't forget the dot at the end! It means copying to the the current directory.
Initialized empty Git repository in /path/to/anotherDir/.git/
The authenticity of host 'haoqi_s.host.com (209.143.128.58)' can't be established.
RSA key fingerprint is 58:9c:96:f9:7c:39:3c:78:07:8c:40:59:a5:4b:34:9b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'haoqi_s.host.com,209.143.128.58' (RSA) to the list of known hosts.
haoqi@haoqi_s.host.com's password:
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.

If instead you get this error below, check that you're in a place where you can do git. In my case, I did "ssh scripts.mit.edu" right before running the git clone, but git doesn't work inside MIT scripts.
Cloning into ....
The authenticity of host 'haoqi_s.host.com (209.143.128.58)' can't be established.
RSA key fingerprint is 58:9c:96:f9:7c:39:3c:78:07:8c:40:59:a5:4b:34:9b.
Are you sure you want to continue connecting (yes/no)? yes
Failed to add the host to the list of known hosts (/current/machines/home/dir/.ssh/known_hosts).
Permission denied (publickey,password).
fatal: The remote end hung up unexpectedly

14. git log
commit d38fbfd10003a28859d5b943b729084bfcf6b78d
Author: HaoQi Li
Date: Sun Feb 13 06:14:54 2011 -0500

brand new start

Yay success!

==========
Edit 3/10:
Github has new tutorials that will help you get started.
1. Set up git in Linux
2. Create a repository
3. Fork a repository
4. Follow a friend

No comments:

Post a Comment