The Go Blog

Git

bantana
24 June 2015

git基本入门

初始化一个git项目

$ git init proja

或者在proja目录下直接初始化当前目录为git目录

$ cd proja
$ git init

在git目录中增加点东西

$ echo "#readme" >> readme.md

把新增加的readme.md文件添加到stage区

$ git add .

把stage区中的提交到本地git版本库中

$ git commit -m "init proja"

正常情况下你应该会得到一个master的分支

$ git branch

* master

通过master分支建立一个开发的分支dev

$ git branch dev master

  dev
* master

切换到dev分支工作

$ git checkout dev
Switched to branch 'dev'

$ git branch

* dev
  master

branch 分支说明

master分支是正式版本发布的主要分支,dev是你平时正常工作的分支,你可以在dev的基础上继续branch一个或多个feature 分支, 当你在dev上完成一个新功能或新版分发布的时候,在dev分支上完成测试,然后切换回master分支,在master分支上merge dev分支上的代码,如下:

在dev分支下:

$ git branch

* dev
  master

增加或修改文件并提交到dev分支上

$ echo "hello" > readme.md
$ git add .
$ git commit -m "add readme.md"

现在分支dev上有一个新的readme.md文件,现在需要把dev分支上的代码merge到master分支上去;

$ git checkout master
$ git merge dev

Updating 11fdf74..e6f0259
Fast-forward
    readme.md | 1 +
    1 file changed, 1 insertion(+)
    create mode 100644 readme.md
  查看当前分支master目录下应该多了个readme.md文件,内容是"hello"

然后把你的本地master修改发布到远程去

$ git push

通过remote命令增加push的remote address:

$ git remote add origin git@github.com:name/repo.git

继续切换回你的dev分支干活去

$ git checkout dev

小结:

一般情况下master分支总是用于产品发布的分支,或者多人协作时候共享的分支;dev总是工作分支,feature分支在完成合并到dev后,总是被删除;feature分支是任意命名的;

一个小工具可以帮助减少输入的问题

$ brew install git-flow

tips

利用hook在post-receive的时候做发布:

$ cat post-receive

#!/bin/sh
GIT_WORK_TREE=/home/pi/sites/mypresent/
export GIT_WORK_TREE
git checkout -f

这样每次接收到push过来的时候就checkout -f到GIT_WORK_TREE目录去.

查找某个文件的log

$ git log homebrew.article

commit e1f073a732d1ab90e94fa45a661134667510933c
Author: bantana <bantana@gmail.com>
Date:   Wed Jun 24 16:59:23 2015 +0800

    fix

commit 89f0d9b3c8e29a365ac8cb4901c2e3d0e2f820e1
Author: bantana <bantana@gmail.com>
Date:   Wed Jun 24 12:45:12 2015 +0800

    fix

commit 2e9c68a7b06a2302440a8071288c663874aa0434
Author: bantana <bantana@gmail.com>
Date:   Wed Jun 24 11:39:08 2015 +0800

    add homebrew.article

通过commit id 恢复到这个文件的history version,只需要取id的前几位一般就够了。

$ git checkout e1f073 homebrew.article

恢复到最新提交的版本:

$ git checkout HEAD^ homebrew.article

增加远程origin

$ git remote add origin mygo:/home/pi/repos/myarticles.git

把remote/master和本地master设置为对应branch,这样可以不带参数直接git pull和git push

$ git branch --set-upstream-to=origin/master master

git cheat

# To set your identify:
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com

# To set your editor:
git config --global core.editor emacs

# To enable color:
git config --global color.ui true

# To stage all changes for commit:
git add --all

# To commit staged changes
git commit -m "Your commit message"

# To edit previous commit message
git commit --amend

# To removed staged and working directory changes
git reset --hard

# To remove untracked files
git clean -f -d

# To remove untracked and ignored files
git clean -f -d -x

# To push to the tracked master branch:
git push origin master

# To push to a specified repository:
git push git@github.com:username/project.git

# To delete the branch "branch_name"
git branch -D branch_name

# To see who commited which line in a file
git blame filename

# To sync a fork with the master repo:
git remote add upstream git@github.com:name/repo.git    # Set a new repo
git remote -v                                           # Confirm new remote repo
git fetch upstream                                      # Get branches
git branch -va                                          # List local - remote branches
git checkout master                                     # Checkout local master branch
git checkout -b new_branch                              # Create and checkout a new branch
git merge upstream/master                               # Merge remote into local repo
git show 83fb499                                        # Show what a commit did.
git show 83fb499:path/fo/file.ext                       # Shows the file as it appeared at 83fb499.
git diff branch_1 branch_2                              # Check difference between branches
git log                                                 # Show all the commits
git status                                              # Show the changes from last commit

.gitconfig include and includeIf

[include]
    path = ~/.privates/git.user
    # ~/.privates/git.user
    # [user]
    #     name = yourname
    #     email = yourname@personal.com
[includeIf "gitdir:~/github/"]
    path = ~/.privates/github
    # ~/.privates/github
    # [github]
    #     name = yourname
    #     email = yourname@gmail.com

Related articles