A Guide for Git starter

I have been use git as a version control tool in my work for about two weeks. For those who are working with a lots of features or bugs at the same time, I strongly suggest you use Git to manage you code. Now I’m gonna talk about how I use git to overcome the first two weeks’ work.

Before you read more about this article, make sure you have git installed on you machine, If you use Mac OS, install Xcode command line tools will include git.

Initial Configuration

When I start to use git I will do some simple configurations before I get my hands dirty.

git config --global user.name "your name"
git config --global user.email "your email"
git config --global core.editor vim
git config --global merge.tool vimdiff

I use vim as the default editor and vimdiff as a merge tool as you see above. If you want to see full configuration list you can type the following command

git config --list

Another useful command I use a lot is the log command, below is a log with graph.

git log --pretty=format:"%h %s" --graph --since=2.week --before="2008-11-01"

As you can see git is really powerful, so if you want to explore the command just use the following command to see the manual.

git (command-name) --help

Start with an project

To start a new project just create a clean directory and type

git init

The command will create a local repository, If you want to upload the repository to remote server, first you have to add remote to the local repository, It’s also easy to do that

git remote add alias remote-git-repository

the alias is a short name for the remote-git-repository in local, you can use git remote -v to see all the remotes

Upload local repository to remote server is also very easy

git push remote-alias branch-name

“git push origin master” will upload to the master of remote origin

Get a copy of remote repository can be down with “git clone remote-repository”
After you clone the repository you can use “git pull” to get updates

Commit a change

git commit files -m "commit message"
git commit -a -m "commit messages"

The most important thing that git has is that you can always commit no matter where you are even in a no network access situation.

Git Branch

This is the most amazing feature git have, feel free to create a branch if you can, I won’t explain the mechanism of the git branch. I just want to say It’s not expensive to create a branch. Below are the most used commands I used in my work.

a) Basic branch commands
Create a branch and switch to that branch(I use that command a lot)

git checkout -b branch-name

List branches

git branch
git branch -v 

Upload code to the remote repository

git push origin branch_name

b) Merge changes in branch to master

Before I explain how this works, I’d like to set two scenarios to help me make it clearer.
Scenario A

1) First you create a branch B and modify a file A
2) Then you commit A on Branch B
3) You switch to master => modification on branch is not visible to master

Scenario B

1) The same as the Scenario A
2) You DON’T commit A on branch B, just leave those change uncommitted
3) Then U switch to master => modifications on file A will still be in you master and uncommitted

I must to say practice in Scenario B should not be encouraged If you want to switch to another branch It’s really good for you to commit your work before you switch.

Merge operation is not so hard either. Imagine you are at master then you want to merge branch_a’s changes to master. You can execute the following command

git merge branch_a

When you try to merge branch, It’s good to keep a clean stat on you branch, git encourage commit so commit a lot.

I will follow some routines when I do merge:

Before merge
pull the latest code from remote server of the current branch (normally it’s master)
After merge
pull the latest code from remote server of the current branch (Yes, it the same as above)
Last step
push merged code to the remote server

Another trick

push an amend commit to the remote server
When you do an amend commit, and you want to push it to the remote server, you can probably follow the steps below.

After you commit, execute a pull command and it probably will cause conflicts on you repository.

git checkout --ours . # checkout our local version of all files
git add -u                # mark all conflicts files as merged
git commit              # commit the merge

Most parts of I learn the last two weeks are above, next time I will dig deeper to how to revert changes, how to resolve conflicts and so on.

Meta

Published: Dec. 9, 2012 Author: ivan Comments:   Word Count: 787
Bookmark and Share

Next: First Taste of Scrapy

Previous: 【翻译】Java I/O vs Java NIO

Tags

git scm

Comments powered by Disqus