mac安装git
$> xcode-select --install

git介绍

1. File Status:

file status

2.1 列出本地分支:

    $ git branch -a

2.2 切换分支:

    $ git checkout branch_name

2.3 删除分支:

    $ git branch -D branch_name

3. 查看当前分支状态:

    $ git status
    On branch master
    Your branch is up-to-date with 'origin/master'.
    
    Changes not staged for commit:
      (use "git reset HEAD <file>..." to unstage)
      (use "git checkout -- <file>..." to discard changes in working directory)
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
    no changes added to commit (use "git add" and/or "git commit -a")

4. 查看当前分支所有commit:

    $ git log
    commit 003
    Author: 
    Date:   
    
        update 1.txt
    
    commit 002
    Author: 
    Date:   
    
        add 1.txt
        
    commit 001
    Author: 
    Date:   
    
        Initial empty repository

也可以通过Git Extensions打开repository(档案库)查看分支

5. git commit

    $ edit hello.c
    $ git rm goodbye.c
    $ git add hello.c
    $ git commit -m "提交的描述信息"

or: git commit -a

Tell the command to automatically stage files that have been modified and deleted, but new files you have not told Git about are not affected.

    $ edit hello.c
    $ rm goodbye.c
    $ git commit -a -m "提交的描述信息"

6. 解决merge冲突

merge conflict

    $ git status | grep unmerged
    unmerged: hello.c
    $ edit hello.c
    $ git add hello.c
    $ git commit

参考网址: Git 分支 - 分支的新建与合并

7.1 上传代码到远程服务器

Remote Server

$ git remote add origin https://github.com/username/jekyll_demo.git
$ git push origin master
$ git remote -v
origin	https://github.com/username/jekyll_demo.git (fetch)
origin	https://github.com/username/jekyll_demo.git (push)

7.2 本地创建分支并提交远程服务器

// 本地创建分支并切换到该分支
$ git branch iss53
$ git checkout iss53

// 为该分支创建远程服务器分支并提交
$ git push origin iss53
To https://github.com/username/jekyll_demo.git
 * [new branch]      iss53 -> iss53
 
// git pull 之前需要执行:
$ git branch --set-upstream-to=origin/iss53 iss53

8.1 下载远程服务器代码到本地

$ git clone git@git.ijie.com:open-api.git

8.2 同步远程服务器代码到本地

$ git pull

git pull 遇到冲突(conflict)

$ git stash
Saved working directory and index state WIP on <branch>: <commit version>
HEAD is now at <commit version>

$ git status
On branch <branch>
Your branch is behind 'origin/<branch>' by 1 commit, and can be fast-forwarded.
  (use "git pull" to update your local branch)
  
$ git pull

$ git stash pop
Dropped refs/stash@{0}

git pull

9. 版本回退

根据--soft --mixed --hard,会对working tree和index和HEAD进行重置:

  1. git reset --mixed:此为默认方式,不带任何参数的git reset,即是这种方式,它回退到某个版本,只保留源码,回退commit和index信息。

  2. git reset --soft:回退到某个版本,只回退了commit的信息,不会恢复到index file一级。如果还要提交,直接commit即可。

  3. git reset --hard:彻底回退到某个版本,本地的源码也会变为上一个版本的内容。

一般情况下回退版本:

再次提交本地代码:

10. 编辑.gitignore

忽略Visual Studio生成时的产生的文件

    *.obj
    *.exe
    *.pdb
    *.user
    *.aps
    *.pch
    *.vspscc
    *_i.c
    *_p.c
    *.ncb
    *.suo
    *.tlb
    *.tlh
    *.bak
    *.cache
    *.ilk
    *.log
    [Bb]in
    [Dd]ebug*/
    *.lib
    *.sbr
    obj/
    [Rr]elease*/
    _ReSharper*/

忽略C++产生的文件

    # Compiled Object files
    *.slo
    *.lo
    *.o
    *.obj
    
    # Precompiled Headers
    *.gch
    *.pch
    
    # Compiled Dynamic libraries
    *.so
    *.dylib
    *.dll
    
    # Fortran module files
    *.mod
    
    # Compiled Static libraries
    *.lai
    *.la
    *.a
    *.lib
    
    # Executables
    *.exe
    *.out
    *.app

参考网址:

  1. git magic

  2. Manual Page

  3. Git教学

  4. git commit简介

  5. git push小结

  6. Git Extensions 2.48 Manual

备注:

Resubmit(git push) code to gerrit
1. copy gerrit checkout 
2. git fetch --all -p 
3. git rebase origin/<branch> 
4. fixed conflicts 
5. git add ... 
6. git rebase --continue 
7. git commit --amend 
8. copy gerrit change-id 
9. git push...


go back commit version: 
1. git log  -> find commit version
2. git reset commit version
3. git pull
4. git add
5. git commit
6. git push

11. Git Stash

参考网址: Git Stash用法