git 本地操作(二、本地分支与合并)

Posted on Posted in git
Tips: 本文创建于2016年5月11日,已超过 2 年,内容或图片可能已经失效!
  • git branch #创建分支
  • git tag #打标签
  • git checkout #切换分支
  • git stash #切换分支之前保存本地的修改
  • git merge
-> liuhonghe@support ~/git ☺ git init git_repo  
Initialized empty Git repository in /home/liuhonghe/git/git_repo/.git/  
-> liuhonghe@support ~/git ☺ cd git_repo   
-> liuhonghe@support ~/git/git_repo git:(master) ☺ echo "Initial commit" > master.txt  
-> liuhonghe@support ~/git/git_repo git:(master) ✗ ☺ git add .  
-> liuhonghe@support ~/git/git_repo git:(master) ✗ ☺ git commit -m "Initial commit on master"  
[master (root-commit) f699140] Initial commit on master  
 Committer: liuhonghe <liuhonghe@localhost.(none)>  
  
 1 files changed, 1 insertions(+), 0 deletions(-)  
 create mode 100644 master.txt  
-> liuhonghe@support ~/git/git_repo git:(master) ☺ echo "Second commit on master" >> master.txt   
-> liuhonghe@support ~/git/git_repo git:(master) ✗ ☺ git add .  
-> liuhonghe@support ~/git/git_repo git:(master) ✗ ☺ git commit -m "Second commit on master"  
[master e93b143] Second commit on master  
 Committer: liuhonghe <liuhonghe@localhost.(none)>  
  
 1 files changed, 1 insertions(+), 0 deletions(-)  
# 添加新分支  
-> liuhonghe@support ~/git/git_repo git:(master) ☺ git branch test  
-> liuhonghe@support ~/git/git_repo git:(master) ☺ git checkout test  
Switched to branch 'test'  
-> liuhonghe@support ~/git/git_repo git:(test) ☺ echo "Initial commit on test" >> master.txt   
-> liuhonghe@support ~/git/git_repo git:(test) ✗ ☺ touch test.txt  
-> liuhonghe@support ~/git/git_repo git:(test) ✗ ☺ git add .  
-> liuhonghe@support ~/git/git_repo git:(test) ✗ ☺ git commit -m "Initial commit on test"  
[test df641d6] Initial commit on test  
 Committer: liuhonghe <liuhonghe@localhost.(none)>  
  
 1 files changed, 1 insertions(+), 0 deletions(-)  
 create mode 100644 test.txt  
-> liuhonghe@support ~/git/git_repo git:(test) ☺ git checkout master  
Switched to branch 'master'  
-> liuhonghe@support ~/git/git_repo git:(master) ☺ git branch  
* master  
  test  
# git 两种标签模式,一个普通tag,一个tag对象  
-> liuhonghe@support ~/git/git_repo git:(master) ☺ git log --oneline --decorate --graph --all   #查看历史记录  
* df641d6 (test) Initial commit on test  
* e93b143 (HEAD, master) Second commit on master  
* f699140 Initial commit on master  
-> liuhonghe@support ~/git/git_repo git:(master) ☺ git tag "v0" f699140  
-> liuhonghe@support ~/git/git_repo git:(master) ☺ git tag  
v0  
-> liuhonghe@support ~/git/git_repo git:(master) ☺ git tag -a "INITIAL_COMMIT" f699140  
-> liuhonghe@support ~/git/git_repo git:(master) ☺ git tag  
INITIAL_COMMIT  
v0  
-> liuhonghe@support ~/git/git_repo git:(master) ☺ git config --global alias.lol "log --oneline --decorate --graph --all"  
-> liuhonghe@support ~/git/git_repo git:(master) ☺ git lol  
* df641d6 (test) Initial commit on test  
* e93b143 (HEAD, master) Second commit on master  
* f699140 (v0, tag: INITIAL_COMMIT) Initial commit on master  
  
-> liuhonghe@support ~/git/git_repo git:(master) ☺ git show v0  
commit f6991405ec8cf44e73d751f7fede05e0a65264e8  
Author: liuhonghe <liuhonghe@localhost.(none)>  
Date:   Wed May 11 14:07:14 2016 +0800  
  
    Initial commit on master  
  
diff --git a/master.txt b/master.txt  
new file mode 100644  
index 0000000..5852f44  
--- /dev/null  
+++ b/master.txt  
@@ -0,0 +1 @@  
+Initial commit  
  
-> liuhonghe@support ~/git/git_repo git:(master) ☺ git show INITIAL_COMMIT  
tag INITIAL_COMMIT  
Tagger: liuhonghe <liuhonghe@support>  
Date:   Wed May 11 14:24:59 2016 +0800  
  
Initial commit  
  
commit f6991405ec8cf44e73d751f7fede05e0a65264e8  
Author: liuhonghe <liuhonghe@localhost.(none)>  
Date:   Wed May 11 14:07:14 2016 +0800  
  
    Initial commit on master  
  
diff --git a/master.txt b/master.txt  
new file mode 100644  
index 0000000..5852f44  
--- /dev/null  
+++ b/master.txt  
@@ -0,0 +1 @@  
+Initial commit  
  
-> liuhonghe@support ~/git/git_repo git:(master) ☺ git checkout v0  
Note: checking out 'v0'.  
  
You are in 'detached HEAD' state. You can look around, make experimental  
changes and commit them, and you can discard any commits you make in this  
state without impacting any branches by performing another checkout.  
  
If you want to create a new branch to retain commits you create, you may  
do so (now or later) by using -b with the checkout command again. Example:  
  
  git checkout -b new_branch_name  
  
HEAD is now at f699140... Initial commit on master  
-> liuhonghe@support ~/git/git_repo git:(f699140) ☺ git checkout -b fix_v0  
Switched to a new branch 'fix_v0'  
-> liuhonghe@support ~/git/git_repo git:(fix_v0) ☺ echo "stash1" >> master.txt  
-> liuhonghe@support ~/git/git_repo git:(fix_v0) ✗ 1☺ git stash save "stash1"  
Saved working directory and index state On fix_v0: stash1  
HEAD is now at f699140 Initial commit on master  
-> liuhonghe@support ~/git/git_repo git:(master) ☺ git stash list   #从其他分支切回来的情况  
stash@{0}: On fix_v0: stash1  
-> liuhonghe@support ~/git/git_repo git:(fix_v0) ☺ git stash pop --index stash@\{0\}    #pop会把选择的stash清除  
# On branch fix_v0  
# Changes to be committed:  
#   (use "git reset HEAD <file>..." to unstage)  
#  
#       modified:   master.txt  
#  
Dropped stash@{0} (26beefc418ce0534740bcfcafe17101a10bb19d2)  
  
-> liuhonghe@support ~/git/git_repo git:(fix_v0) ✗ ☺ git stash save "stash1"   #恢复回去  
Saved working directory and index state On fix_v0: stash1  
HEAD is now at f699140 Initial commit on master  
-> liuhonghe@support ~/git/git_repo git:(fix_v0) ☺ git stash list  
stash@{0}: On fix_v0: stash1  
-> liuhonghe@support ~/git/git_repo git:(fix_v0) ☺ git stash apply --index stash@\{0\}                                                                                                                                             
# On branch fix_v0  
# Changes to be committed:  
#   (use "git reset HEAD <file>..." to unstage)  
#  
#       modified:   master.txt  
#  
-> liuhonghe@support ~/git/git_repo git:(fix_v0) ✗ ☺ cat master.txt   
Initial commit  
stash1  
-> liuhonghe@support ~/git/git_repo git:(fix_v0) ✗ ☺ git stash list  
stash@{0}: On fix_v0: stash1  
-> liuhonghe@support ~/git/git_repo git:(fix_v0) ✗ ☺ git stash drop stash@\{0\}   
Dropped stash@{0} (e47e2f40848e9b05561be1a2bc7cc4a6cd06256f)  
-> liuhonghe@support ~/git/git_repo git:(fix_v0) ✗ ☺ git stash list  
-> liuhonghe@support ~/git/git_repo git:(fix_v0) ✗ ☺ git stash clear    #清理所有  
  
# 分支合并  
-> liuhonghe@support ~/git/git_repo git:(fix_v0) ✗ ☺ git stash save  
Saved working directory and index state WIP on fix_v0: f699140 Initial commit on master  
HEAD is now at f699140 Initial commit on master  
-> liuhonghe@support ~/git/git_repo git:(fix_v0) ☺ git stash list  
-> liuhonghe@support ~/git/git_repo git:(fix_v0) ☺ git checkout master  
Switched to branch 'master'  
-> liuhonghe@support ~/git/git_repo git:(master) ☺ git checkout -b test_merge  
Switched to a new branch 'test_merge'  
-> liuhonghe@support ~/git/git_repo git:(test_merge) ☺ echo "Initial commit on test_merge" >> master.txt  
-> liuhonghe@support ~/git/git_repo git:(test_merge) ✗ ☺ git add .  
-> liuhonghe@support ~/git/git_repo git:(test_merge) ✗ ☺ git commit -m "Initial commit on test_merge"  
[test_merge 8bb97f0] Initial commit on test_merge  
 1 files changed, 3 insertions(+), 0 deletions(-)  
 create mode 100644 master.txt  
-> liuhonghe@support ~/git/git_repo git:(test_merge) ☺ git lol  
* 8bb97f0 (HEAD, test_merge) Initial commit on test_merge  
* 78cd3a1 Initial commit on test_merge  
| *   5b37bf7 (refs/stash) WIP on fix_v0: f699140 Initial commit on master  
| |\    
| | * ae71b98 index on fix_v0: f699140 Initial commit on master  
| |/    
| | * df641d6 (test) Initial commit on test  
| |/    
|/|     
* | e93b143 (master) Second commit on master  
|/    
* f699140 (v0, tag: INITIAL_COMMIT, fix_v0) Initial commit on master  
-> liuhonghe@support ~/git/git_repo git:(test_merge) ☺ git checkout master  
Switched to branch 'master'  
-> liuhonghe@support ~/git/git_repo git:(master) ☺ git merge test_merge  
Updating e93b143..8bb97f0  
Fast-forward  
 master.txt |    1 +  
 1 files changed, 1 insertions(+), 0 deletions(-)  
-> liuhonghe@support ~/git/git_repo git:(master) ☺ git lol  
* 8bb97f0 (HEAD, test_merge, master) Initial commit on test_merge  
* 78cd3a1 Initial commit on test_merge  
* 19070c8 Initial commit on test_mergi  
| *   5b37bf7 (refs/stash) WIP on fix_v0: f699140 Initial commit on master  
| |\    
| | * ae71b98 index on fix_v0: f699140 Initial commit on master  
| |/    
| | * df641d6 (test) Initial commit on test  
| |/    
|/|     
* | e93b143 Second commit on master  
|/    
* f699140 (v0, tag: INITIAL_COMMIT, fix_v0) Initial commit on master  
-> liuhonghe@support ~/git/git_repo git:(master) ☺ git merge test  
Auto-merging master.txt  
CONFLICT (content): Merge conflict in master.txt  
Automatic merge failed; fix conflicts and then commit the result.  
-> liuhonghe@support ~/git/git_repo git:(master) ✗ 1☺ cat master.txt   
Initial commit  
Second commit on master  
<<<<<<< HEAD  
Initial commit on test_merge  
=======  
Initial commit on test  
>>>>>>> test  
# 处理冲突,重新add->commit  

Leave a Reply

Your email address will not be published. Required fields are marked *

3 × three =