Git教程小结

来源:互联网 发布:c 编写组态软件 编辑:程序博客网 时间:2024/06/05 01:18

两个小时看完了GIT教程,这个教程写的很不错,风趣幽默,不仅讲怎么用,还讲了在什么情境下用。

http://www.liaoxuefeng.com/

以下小结为了方便自己查阅

1.setting
    $ git config --global user.name "Your Name"
    $ git config --global user.email "email@example.com"

2.establish git respository
    build a folder and cd in
    $ git init

3.add file to respository
    $ git add readme.txt

4.commit
    $ git commit -m "wrote a readme file"

5.check modification
    $ git status
    $ git diff
    
    $ git diff HEAD -- readme.txt

6.version recall
    $ git log --pretty=oneline
    $ git reset --hard HEAD^     //HEAD~100
    $ git reset --hard 3628164   //part of version id

    $ git reflog

7.discard modification
    $ git checkout -- readme.txt
    $ git reset HEAD readme.txt

8.remote respository
    $ ssh-keygen -t rsa -C "youremail@example.com"
    github.com
    add sshkey
    ~/.ssh/id_rsa.pub

9.remote relate and push
#Create a new repository on the command line
    touch README.md
    git init
    git add README.md
    git commit -m "first commit"
    git remote add origin git@github.com:stech1117/learngit.git
    git push -u origin master

#Push an existing repository from the command line
    git remote add origin git@github.com:stech1117/learngit.git
    git push -u origin master

    use push after commit to update to remote server

10.clone
    $ git clone git@github.com:stech1117/gitskills.git

11.branch
    $ git checkout -b dev
    --equals to following:
        $ git branch dev
        $ git checkout dev
    $ git branch

    $ git merge dev
    $ git branch -d dev

12.handle confliction
    $ git status
    #manually fix confliction if there have some
#show branches
    $ git log --graph --pretty=oneline --abbrev-commit

13.branches control strategy
    $ git merge --no-ff -m "merge with no-ff" dev

分支策略
在实际开发中,我们应该按照几个基本原则进行分支管理:
首先,master分支应该是非常稳定的,也就是仅用来发布新版本,平时不能在上面干活;
那在哪干活呢?干活都在dev分支上,也就是说,dev分支是不稳定的,到某个时候,比如1.0版本发布时,再把dev分支合并到master上,在master分支发布1.0版本;
你和你的小伙伴们每个人都在dev分支上干活,每个人都有自己的分支,时不时地往dev分支上合并就可以了。
合并分支时,加上--no-ff参数就可以用普通模式合并,合并后的历史有分支,能看出来曾经做过合并,而fast forward合并就看不出来曾经做过合并。

14.stash to store current workspace
    $ git stash
    $ git stash list
#then it's safe to fix bug in another branch

    $ git stash pop
        #multiple stash
        $ git stash apply stash@{0}
15.delete unmerged branch
    $ git branch -D feature-vulca

16.co-coding
    $ git remote -v
并不是一定要把本地分支往远程推送,那么,哪些分支需要推送,哪些不需要呢?
    master分支是主分支,因此要时刻与远程同步;
    dev分支是开发分支,团队所有成员都需要在上面工作,所以也需要与远程同步;
    bug分支只用于在本地修复bug,就没必要推到远程了,除非老板要看看你每周到底修复了几个bug;
    feature分支是否推到远程,取决于你是否和你的小伙伴合作在上面开发。

    $ git branch --set-upstream dev origin/dev
    $ git pull

多人协作的工作模式通常是这样:
    1.首先,可以试图用git push origin branch-name推送自己的修改;
    2.如果推送失败,则因为远程分支比你的本地更新,需要先用git pull试图合并;
    3.如果合并有冲突,则解决冲突,并在本地提交;
    4.没有冲突或者解决掉冲突后,再用git push origin branch-name推送就能成功!
如果git pull提示“no tracking information”,则说明本地分支和远程分支的链接关系没有创建,用命令git branch --set-upstream branch-name origin/branch-name。
这就是多人协作的工作模式,一旦熟悉了,就非常简单。

17.tag
    #1
    $ git tag v1.0
    #2
    $ git log --pretty=oneline --abbrev-commit
    $ git tag v0.9 6224937
    #$ git tag -a v0.1 -m "version 0.1 released" 3628164
    #3
    $ git tag
    $ git show tagname

#tag delete
    $ git tag -d v0.1
    $ git push origin v1.0
    #$ git push origin --tags
#if tag already on origin
    $ git tag -d v0.9
    $ git push origin :refs/tags/v0.9
18.others
    $ git config --global alias.unstage 'reset HEAD'
    $ git config --global alias.last 'log -1'
    $ git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"




0 0
原创粉丝点击