GitHub实际操作


###1. 基本操作

  • git init ——初始化仓库

$ mkdir git-tutorial
$ cd git-tutorial
$ git init
Initialized empty Git repository in F:/GitHub/git-tutorial/.git/
  • git status——查看仓库的状态

$ git status
On branch master

Initial commit

nothing to commit (create/copy files and use "git add" to track)
$ touch README.md
$ git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        README.md

nothing added to commit but untracked files present (use "git add" to track)
  • git add——向暂存区中添加文件

$ git add README.md
$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   README.md
  • git commit——保存仓库的历史记录

$ git commit
  • git log——查看提交日志

$ git log
commit 27e47fef9b00ccb4077d15c388b1f9b042ed24ca
Author: zhaojun <zhaojunhhu@gmail.com>
Date:   Mon Mar 7 17:58:11 2016 +0800

    test for git-tutorial

$ git log --pretty=short
commit 27e47fef9b00ccb4077d15c388b1f9b042ed24ca
Author: zhaojun <zhaojunhhu@gmail.com>

    test for git-tutorial

$ git log -p
commit 27e47fef9b00ccb4077d15c388b1f9b042ed24ca
Author: zhaojun <zhaojunhhu@gmail.com>
Date:   Mon Mar 7 17:58:11 2016 +0800

    test for git-tutorial

diff --git a/README.md b/README.md
new file mode 100644
index 0000000..e69de29
  • git diff——查看更改前后的区别

$ git diff
diff --git a/README.md b/README.md
index e69de29..0ba0efb 100644
--- a/README.md
+++ b/README.md
@@ -0,0 +1 @@
+#  Git教程
\ No newline at end of file  

###2. 分支的操作

  • git branch——显示分支一览表

$ git branch
* master
  • git checkout——创建切换分支

创建feature-A分支,并将当前分支切换为feature-A分支。这时再查看分支列表,会显示在feature-A分支下。

$ git checkout -b feature-A
Switched to a new branch 'feature-A'

$ git branch feature-A
$ git checkout feature-A
Switched to branch 'feature-A'

$ git branch
* feature-A
  master 
  • git merge——合并分支

$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
$ git merge --no-ff feature-A
Merge made by the 'recursive' strategy.
 README.md | 3 +++
 1 file changed, 3 insertions(+)
  • git log --graph——以图表方式查看分支

$ git log --graph
*   commit 99611752247ff104c98f86bd64cda1fa07dba6f3
|\  Merge: cc7f6ed 2411015
| | Author: zhaojunhhu <zhaojunhhu@gmail.com>
| | Date:   Mon Mar 14 00:04:51 2016 +0800
| | 
| |     Merge branch 'feature-A'
| |   
| * commit 2411015b6ad32ae62a83624c0a058c3cbb1797aa
|/  Author: zhaojunhhu <zhaojunhhu@gmail.com>
|   Date:   Sun Mar 13 23:51:58 2016 +0800
|   
|       Add feature-A
|  
* commit cc7f6ed7a65c6cd2e65770bbf124bd80471473e5

###3. 更改提交的操作

  • git reset——回溯历史版本

$ git reset --hard cc7f6ed7a65c6cd2e65770bbf124bd80471473e5
HEAD is now at cc7f6ed Add php script
  • 创建fix-B分支

$ git checkout -b fix-B
Switched to a new branch 'fix-B'

$ git status
On branch fix-B
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")

$ git add README.md 

$ git commit -m "Fix B"
[fix-B 34e6ca6] Fix B
 1 file changed, 4 insertions(+)
  • 推进至feature-A分支合并后的状态

$ git checkout master
Switched to branch 'master'

$ git reset --hard 9961175
HEAD is now at 9961175 Merge branch 'feature-A'
  • 消除冲突

$ git merge --no-ff fix-B
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.

Last updated