Wednesday, November 10, 2010

Simple Guide for Git

This is a simple guide for git to tell you how to use git to manage your source code. Here, we assume that your git server and your local computer are different. We will teach you how to initialize your git server, put your source to the server, download the source from the server, and update your changes to the server step by step. The git commands used here, however, are all very easy and basic, but they can give you a clear picture of how gits work.

Server: 192.168.0.100
User on the server: git
Project: spsw
User on the client: Wayne


  • On the server
    1. Login the git server
      ssh git@192.168.0.100
    2. Create a directory for the project "spsw". A repository on server should be bare, and by convention, a bare repository is named with ".git" suffix.
      mkdir spsw.git
      cd spsw.git
    3. Initialize a bare repository.
      git init --bare
  • On the Client
    1. Setup you personal information.
      git config --global user.name "Wayne"
      git config --global user.email "wayne@gmail.com"
    2. Create a non-bare repository. There are two ways to do this.
      1. By cloning the source from the server. (This assumes that the server has the source code, but so far we haven't put our source to the server - the server doesn't has the source code)
        1. git clone git@192.168.0.100:/home/git/spsw.git
      2. By creating a new project and then putting to the server.
        1. Create a directory for the project spsw. (".git" suffix is not needed because it's a non-bare repository)
          mkdir spsw
          cd spsw
        2. Initialize a non-bare repositry.
          git init
        3. Copy your source here
        4. Add all the files. ("git add ." is the choice for this case)
          git add .    # Add new and modified without deleted
          git add -u   # Add modified and updated without new
          git add -A  # git add . && git add -u
        5. Show what you've added or changed if you want.
          git diff
        6. Commit the changes.
          git commit -a -m "changes description"
        7. Add the git server source and name it "origin"
          git remote add origin git@192.168.0.100:/home/git/spsw.git
        8. Put the source code in current branch to the server "origin" and branch "master".
          git push origin master
    3. Upate the source from the server.
      git fetch    # download the source to branch "origin/master"
      git merge   # merge "origin/master" to the current branch.
      git pull      # git fetch && git merge
    4. Some useful commands operating branches
      1. Create a new branch
        git branch new_branch
      2. Swith to new_branch
        git checkout new_branch
      3. Delete new_branch
        git branch -D new_branch
      4. show the list of all the branches
        git branch -a
        git show-branch
    5. Other useful commands
      1. View all you've commit
        git log
      2. Show a commit detail
        git show commit_number
      3. Give a commit a tag like a name
        git tag -m "version 1.0" 1.0 commit_number
        git show 1.0
  • Reference
    1. http://progit.org/book/zh/ch1-1.html
    2. http://zh-tw.whygitisbetterthanx.com/#cheap-local-branching
    3. http://gitref.org/
    4. http://tw.myblog.yahoo.com/stevegigijoe/article?mid=173&next=172&l=f&fid=8
  • Books
    1. Version Control with Git
    2. Pro Git
Wayne

No comments:

Post a Comment