美文网首页
GIT 深入探索

GIT 深入探索

作者: WeekDiffculty | 来源:发表于2017-01-23 10:59 被阅读0次

git

1.创建仓库路径

$ mkdir learngit
$ cd learngit
$ pwd
/Users/michael/learngit

2.初始化仓库

$ git init
Initialized empty Git repository in /Users/michael/learngit/.git/
bfdeMacBook-Pro:lib bf$ ls -ah
.   ..  .git
bfdeMacBook-Pro:lib bf$
  • 查看隐藏文件,多了.git
git add 
git commit 
git diff 查看文件的difference
git status

3.版本回退

git log
git log --pretty=oneline  减少输出信息
屏幕快照 2017-01-20 下午3.15.02.png
HEAD 当前版本
HEAD^ 上一个版本
HEAD^^ 上上个版本
HEAD~100 往上100个版本
  • 回到上一个版本
bfdeMacBook-Pro:lib bf$ git reset --hard HEAD^
HEAD is now at e4a2839 fasdf
bfdeMacBook-Pro:lib bf$

--hard?

bfdeMacBook-Pro:lib bf$ git reset --hard f1708d0
HEAD is now at f1708d0 dsfa
bfdeMacBook-Pro:lib bf$ git log
commit f1708d0dd521f878b07a276e8796a4237b391be2
Author: zbf <bf@bfdeMacBook-Pro.local>
Date:   Fri Jan 20 15:06:23 2017 +0800

    dsfa

commit e4a28390bd0b72c5414d4bf67d7b07d3c80fda59
Author: zbf <bf@bfdeMacBook-Pro.local>
Date:   Fri Jan 20 15:01:59 2017 +0800

    fasdf
bfdeMacBook-Pro:lib bf$
  • 回退到上一个版本后,最后一个版本会在log里消失,但可以使用 git reset --hard f1708d0 回到制定版本,commit id 前几位就可以了,git会自动查找,

 现在,你回退到了某个版本,关掉了电脑,第二天早上就后悔了,想恢复到新版本怎么办?找不到新版本的commit id怎么办?
在Git中,总是有后悔药可以吃的。当你用* git reset --hard HEAD^*回退到**版本时,再想恢复到append GPL,就必须找到append GPL的commit id。Git提供了一个命令git reflog用来记录你的每一次命令:
bfdeMacBook-Pro:lib bf$ git reflog
f1708d0 HEAD@{0}: reset: moving to f1708d0
e4a2839 HEAD@{1}: reset: moving to HEAD^
f1708d0 HEAD@{2}: commit: dsfa
e4a2839 HEAD@{3}: commit (initial): fasdf
bfdeMacBook-Pro:lib bf$
  • 重返未来,用git reflog查看历史,以便确定要回到未来的那个版本。

管理版本

bfdeMacBook-Pro:lib bf$ git diff HEAD -- aaa.txt
diff --git a/aaa.txt b/aaa.txt
index a79beab..6f150ec 100644
--- a/aaa.txt
+++ b/aaa.txt
@@ -1,2 +1,3 @@
 helloworl
 fdsaffd
+fdaf
bfdeMacBook-Pro:lib bf$
  • git diff HEAD -- aaa.txt用来查看工作区与版本库最新版的差别

撤销修改

bfdeMacBook-Pro:lib bf$ git checkout -- aaa.txt
  • git checkout -- aaa.txt 撤销对文件的修改 总之,就是让这个文件回到最近一次 git commitgit add时的状态。 -- 很重要
bfdeMacBook-Pro:lib bf$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   aaa.txt

bfdeMacBook-Pro:lib bf$ git reset HEAD aaa.txt
Unstaged changes after reset:
M   aaa.txt
bfdeMacBook-Pro:lib bf$ ls
aaa.txt
bfdeMacBook-Pro:lib bf$ git status
On branch master
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:   aaa.txt

no changes added to commit (use "git add" and/or "git commit -a")
bfdeMacBook-Pro:lib bf$
  • git reset HEAD aaa.txt 把已添加暂存区的文件撤销

删除文件

$ git rm test.txt 从版本库中删除,可以恢复
rm 'test.txt'
$ git commit -m "remove test.txt"
[master d17efd8] remove test.txt
 1 file changed, 1 deletion(-)
 delete mode 100644 test.txt
  • 这样就删除了,如果删错了:
git checkout test.txt //就恢复了

远程仓库

$ ssh-keygen -t rsa -C "youremail@example.com"

分支操作

  • 创建新分支
bfdeMacBook-Pro:lib bf$ git checkout -b dev
Switched to a new branch 'dev'
bfdeMacBook-Pro:lib bf$
  • 提交到新分支
bfdeMacBook-Pro:lib bf$ git commit -m "D"
On branch dev
nothing to commit, working tree clean
bfdeMacBook-Pro:lib bf$
  • 切换到master分支
bfdeMacBook-Pro:lib bf$ git checkout master
Switched to branch 'master'
bfdeMacBook-Pro:lib bf$
  • 合并
bfdeMacBook-Pro:lib bf$ git merge dev
Already up-to-date.
bfdeMacBook-Pro:lib bf$
  • 删除分支
bfdeMacBook-Pro:lib bf$ git branch -d dev
Deleted branch dev (was 78b0a3a).
bfdeMacBook-Pro:lib bf$
  • 小结
小结

Git鼓励大量使用分支:

查看分支:git branch

创建分支:git branch <name>

切换分支:git checkout <name>

创建+切换分支:git checkout -b <name>

合并某分支到当前分支:git merge <name>

删除分支:git branch -d <name>

解决冲突

  • 创建新分支
git checkout -b new
Switched to a new branch 'new'
bfdeMacBook-Pro:lib bf$
  • 修改内容,重新提交
  • 切换到 master
  • 修改内容,提交
  • 执行命令合并,无法快速合并
bfdeMacBook-Pro:lib bf$ git merge new
Auto-merging aaa.txt
CONFLICT (content): Merge conflict in aaa.txt
Automatic merge failed; fix conflicts and then commit the result.
bfdeMacBook-Pro:lib bf$
屏幕快照 2017-01-23 上午10.40.16.png
  • Git用<<<<<<<,=======,>>>>>>>标记出不同分支的内容,我们手动解决冲突,保存提交
屏幕快照 2017-01-23 上午10.45.19.png
  • 用带参数的git log也可以看到分支的合并情况:
bfdeMacBook-Pro:lib bf$ git log --graph --pretty=oneline --abbrev-commit
*   9c906c7 new
|\
| * b5c069c fsdafa
* | 7601d44 dsfafdsfaadsfsdf
|/
*   ddd78ff das
|\
| * bd2e8d0 fsd
| * 7840261 fdsaf
| * bf660c7 AND simple
* | 79e9a59 dsfdasf
* | 1163c06 simple
|/
* 78b0a3a fasf
* 4c929de fdsfa
* 39c936f dsaf
* d0462d7 dfafs
* 6cef978 remove test.txt
* 76f6daf add test
* 49dae35 dfs
* f1708d0 dsfa
* e4a2839 fasdf
bfdeMacBook-Pro:lib bf$
  • 最后删除分支,
bfdeMacBook-Pro:lib bf$ git branch -d new
Deleted branch new (was b5c069c).
bfdeMacBook-Pro:lib bf$

-待续

相关文章

网友评论

      本文标题:GIT 深入探索

      本文链接:https://www.haomeiwen.com/subject/ptlabttx.html