版本控制工具 GIT

Git:存档;就是你每次提交一次,Git就咔嚓一下给你的所有文件拍个照,相当于保存一个副本到隐藏的区域,也就是它帮你隐藏了所有之前的论文版本,只给你看最近修改完成的,也可以瞬间回到之前的任意版本。

分布式版本控制:集中式的版本控制系统不同,分布式的版本控制在所有开发者手里都有一套完整的代码库,也就是只要下载过的开发者都有一个完整的副本,这样带来的好处就是只要这几个人的电脑不同时坏硬盘,这份工程一般是不会丢的,而坏处就是这个项目将会变得很自由,因为所有人都有完整的工程。 

Git的文件状态:(文件的状态跟文件所在的位置有关:工作区 暂存区 版本库 远程库)

  untracked:未跟踪 

  staged:已暂存

  committed:已提交

  modified:已修改

  ignore:已忽略

//初次运行Git前的配置:每次 Git 提交时都会引用这两条信息,说明是谁提交了更新,所以会随更新内容一起被永久纳入历史记录;

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git config --global user.name "Dillenii"
kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git config --global user.email 1264564847@qq.com
//本地文件编辑
kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit
$git config --global user.name "Dillenii" $ git init
//把这个目录变成Git可以管理的仓库,自动创建分支master Initialized empty Git repository in C:/Users/kf/Desktop/htdocs/learnGit/.git/ kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master) $ git status //查看目录状态On branch master Initial commit nothing to commit (create/copy files and use "git add" to track) //在目录内新建hello.txt kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master) $ git status On branch master Initial commit Untracked files: //有未追踪文件 (use "git add <file>..." to include in what will be committed) hello.txt nothing added to commit but untracked files present (use "git add" to track) kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master) $ git add hello.txt //添加hello.txt文件到“暂存区” kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master) $ git commit -m "add a hello file" // 添加hello.txt文件到“版本库”,外加备注 [master (root-commit) c6831b7] add a hello file 1 file changed, 1 insertion(+) create mode 100644 hello.txt //在目录内新建 file1.txt file2.txt file3.txt$ git add file1.txt file2.txt file3.txt $ git commit -m "add 3 files“. //可一次性把暂存区的文件添加到版本库 //修改文件hello.txt kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master) $ 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: hello.txt //已修改 no changes added to commit (use "git add" and/or "git commit -a") kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master) $ git diff hello.txt //查看具体修改了什么内容 diff --git a/hello.txt b/hello.txt index c57eff5..792072d 100644 --- a/hello.txt+++ b/hello.txt @@ -1 +1,2 @@-Hello World! //修改前的内容\ No newline at end of file+Hello World! +hello hello hello... //修改后的内容\ No newline at end of file kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master) $ git add hello.txt //修改完后重新add至缓存区 kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master) $ git commit -m "change hello.txt" //提交至版本库[master b8796be] change hello.txt 1 file changed, 2 insertions(+), 1 deletion(-) kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master) $ git status On branch master nothing to commit, working tree clean //为干净状态 //再次修改hello.txt kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master) $ git add hello.txt kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master) $ git commit -m "change hello.txt 2" [master 6dae020] change hello.txt 2 1 file changed, 2 insertions(+), 1 deletion(-) kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master) $ git log //查看版本commit 6dae020386b37a3e8b7024d080726604d55007e2 Author: yangqian <1264564847@qq.com> Date: Mon Feb 13 14:51:16 2017 +0800 change hello.txt 2 commit b8796be6c8e291355d3444bc5bcb9df6c481a831 Author: yangqian <1264564847@qq.com> Date: Mon Feb 13 14:44:34 2017 +0800 change hello.txt commit d76818057685d4021c74aa64c068134e3d41ec7c Author: yangqian <1264564847@qq.com> Date: Mon Feb 13 14:38:30 2017 +0800 add 3 files commit c6831b7b02a285b6ff45ba35217322911b222dfd Author: yangqian <1264564847@qq.com> Date: Mon Feb 13 14:29:23 2017 +0800 add a hello file kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master) $ git log --pretty=oneline //一个版本放在一行显示 6dae020386b37a3e8b7024d080726604d55007e2 change hello.txt 2 b8796be6c8e291355d3444bc5bcb9df6c481a831 change hello.txt d76818057685d4021c74aa64c068134e3d41ec7c add 3 files c6831b7b02a285b6ff45ba35217322911b222dfd add a hello file kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master) $ git reset --hard HEAD^//回到上一个版本//用HEAD表示当前版本,上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本HEAD~100 HEAD is now at b8796be change hello.txt // b8796be 为commit id kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master) $ git log --pretty=oneline b8796be6c8e291355d3444bc5bcb9df6c481a831 change hello.txt d76818057685d4021c74aa64c068134e3d41ec7c add 3 files c6831b7b02a285b6ff45ba35217322911b222dfd add a hello file kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master) $ git reset --hard 6dae //回到版本号为6dae,版本号不一定要写全 HEAD is now at 6dae020 change hello.txt 2 kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master) $ cat hello.txt //查看hello.txt文件内容 Hello World! hello hello hello... hello kitty :) kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master) $ git reflog //查看操作历史 6dae020 HEAD@{0}: reset: moving to 6dae b8796be HEAD@{1}: reset: moving to HEAD^ 6dae020 HEAD@{2}: commit: change hello.txt 2 b8796be HEAD@{3}: commit: change hello.txt d768180 HEAD@{4}: commit: add 3 files c6831b7 HEAD@{5}: commit (initial): add a hello file
//文件内容修改,添加stupid kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master) $ cat hello.txt Hello World! hello hello hello... hello kitty :) stupid kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master) $ git checkout -- hello.txt //撤销修改 git checkout 还不太懂,遇到问题在学习 //上步已无stupid,再次添加stupid kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master) $ cat hello.txt Hello World! hello hello hello... hello kitty :) kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master) $ git add hello.txt kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master) $ git commit -m "add stupid in hello.txt" [master 586cdb6] add stupid in hello.txt 1 file changed, 2 insertions(+), 1 deletion(-)
//提交后撤销,无作用;只能撤销未提交的文件内容 kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master) $ git checkout -- hello.txt kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master) $ cat hello.txt Hello World! hello hello hello... hello kitty :) stupid kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master) $ git rm file2.txt rm 'file2.txt' //删除file2.txt kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master) $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) deleted: file2.txt modified: hello.txt kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master) $ git commit -m "change at last" [master 84d6496] change at last 2 files changed, 1 insertion(+), 2 deletions(-) delete mode 100644 file2.txt

//分支部分

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git checkout -b dev //表示创建并切换 = $ git branch dev $git checkout dev 
Switched to a new branch 'dev'

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)
$ git branch //查看分支
* dev
master

//修改hello.txt 后加you and i are stupid fool;

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)

$ git add hello.txt

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)
$ git commit -m "stupid fool"
[dev e7c3c0e] stupid fool
1 file changed, 1 insertion(+), 1 deletion(-)

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)
$ git checkout master //切换回master分支
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
Switched to branch 'master'

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ cat hello.txt//查看hello.txt内容未改变
Hello World!
hello hello hello...
hello kitty :)
stupid pabo
kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git merge dev//把dev分支合并到当前分支
Updating 8e18319..e7c3c0e
Fast-forward
hello.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ cat hello.txt//再查看
Hello World!
hello hello hello...
hello kitty :)
you and i are stupid fool
kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git branch -d dev//删除dev分支
Deleted branch dev (was e7c3c0e).

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git branch //查看分支
* master

//分支冲突

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git checkout -b dev
Switched to a new branch 'dev'

//修改hello.txt:stupid fool at dev

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)

$ git add hello.txt

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)
$ git commit -m "change at dev"
[dev 2e49401] change at dev
1 file changed, 1 insertion(+), 1 deletion(-)

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)
$ git checkout master
Your branch is ahead of 'origin/master' by 1 commits.//提示当前的分支比远程的分支多1个
(use "git push" to publish your local commits)
Switched to branch 'master'

//修改hello.txt:stupid fool at master

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git add hello.txt

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git commit -m "change at master"
[master a662358] change at master
1 file changed, 1 insertion(+), 1 deletion(-)

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git merge dev//合并分支
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
Automatic merge failed; fix conflicts and then commit the result.//分支冲突,不能合并

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master|MERGING)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commits.
(use "git push" to publish your local commits)
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)

Unmerged paths:
(use "git add <file>..." to mark resolution)

both modified: hello.txt

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

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master|MERGING)
$ vi hello.txt//vi 文件名 进去编辑页面

[1]+ Stopped vi hello.txt

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master|MERGING)
$ cat hello.txt
Hello World!
hello hello hello...
hello kitty :)
<<<<<<< HEAD
stupid fool at master
=======
stupid fool at dev
>>>>>>> dev

//修改完,在编辑页面底部":wq"为保存并退出编辑页面

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master|MERGING)
$ git add hello.txt

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master|MERGING)
$ git commit -m "change all"
[master c3e7db4] change all

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ cat hello.txt
Hello World!
hello hello hello...
hello kitty :)
stupid fool at branch

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git log --graph --pretty=oneline --abbrev-commit //查看分支结构
* c3e7db4 change all
|\
| * 2e49401 change at dev
* | a662358 change at master
|/
* e7c3c0e stupid fool

……

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git branch -d dev
Deleted branch dev (was 2e49401).

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git checkout -b dev
Switched to a new branch 'dev'

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)
$ git add hello.txt

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)
$ git commit -m "add merge"
[dev 7e1479a] add merge
1 file changed, 1 insertion(+), 2 deletions(-)

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)
$ git checkout master
Your branch is ahead of 'origin/master' by 9 commits.
(use "git push" to publish your local commits)
Switched to branch 'master'

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git merge --no-ff -m "merge with no-ff" dev//如果要强制禁用Fast forward模式,Git就会在merge时生成一个新的commit,这样,从分支历史上就可以看出分支信息
Merge made by the 'recursive' strategy.
hello.txt | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git log --graph --pretty=oneline --abbrev-commit
* d2bc792 merge with no-ff
|\
| * 7e1479a add merge
|/
* c3e7db4 change all
|\
| * 2e49401 change at dev
* | a662358 change at master
|/
* 6bbf19f change hello.txt same as dev

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git branch -d dev
Deleted branch dev (was 7e1479a).

//测试用Fast forward模式

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git checkout -b dev
Switched to a new branch 'dev'

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)
$ git add hello.txt

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)
$ git commit -m "add 2"
[dev b020468] add 2
1 file changed, 1 insertion(+), 1 deletion(-)

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)
$ git checkout master
Your branch is ahead of 'origin/master' by 11 commits.
(use "git push" to publish your local commits)
Switched to branch 'master'

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git merge dev //合并看不出来曾经做过合并
Updating d2bc792..b020468
Fast-forward
hello.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git log --graph --pretty=oneline --abbrev-commit
* b020468 add 2
* d2bc792 merge with no-ff
|\
| * 7e1479a add merge
|/
* c3e7db4 change all
|\
| * 2e49401 change at dev
* | a662358 change at master
|/
* 6bbf19f change hello.txt same as dev

//合并分支时,加上--no-ff参数就可以用普通模式合并,合并后的历史有分支,能看出来曾经做过合并,而fast forward合并就看不出来曾经做过合并。

 

//其他分支出现bug,当前分支工作未完成,不想提交:先把当前分支“储藏”起来,然后去其他分支修复bug,修复后等再回到当前分支恢复现场后继续工作

//当前分支hello.txt hello2.txt文件未提交,还未完成

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 12 commits.
(use "git push" to publish your local commits)
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: hello.txt

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

hello2.txt

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

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git stash //先把先把当前分支master“储藏”起来
Saved working directory and index state WIP on master: b020468 add 2
HEAD is now at b020468 add 2

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git checkout dev
Switched to branch 'dev'

//切换到分支dev,修复分支上的bug:1.创建临时分支;2.在临时分支上修改bug,再使用$ git merge --no-ff -m "merged bug fix" 临时分支名 合并,并删除分支

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)
$ git checkout master
Your branch is ahead of 'origin/master' by 12 commits.
(use "git push" to publish your local commits)
Switched to branch 'master'

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git stash list //查看储存列表
stash@{0}: WIP on master: b020468 add 2

//若你多次stash,恢复的时候,先用git stash list查看,然后恢复指定的stash,用命令:$ git stash apply stash@{0}

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git stash pop//等于用git stash apply恢复,再用git stash drop来删除;

On branch master
Your branch is ahead of 'origin/master' by 12 commits.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

new file: hello2.txt

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: hello.txt

Dropped refs/stash@{0} (6ad143ae0fcc02544289f84e11fbcda312cdecd3)

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git stash list//再查看无储存内容

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git add hello.txt hello2.txt

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git commit -m "change at last"
[master ea6ce55] change at last
1 file changed, 4 insertions(+)
create mode 100644 hello2.txt

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 14 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean

//若分支未合并要删除,必须强制删除:$ git branch -D 分支名

//标签:就是指向某个commit的指针,但是commit id是一串乱七八糟的数字不好找,换一个办法:按照tag v1.2查找commit就可以了

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git tag v1.0//给分支master打一个新标签

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git tag//查看所有标签
v1.0

//要给之前的commit id打标签:比方说要对add merge这次提交打标签,它对应的commit id是7e1479a

 

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git log --pretty=oneline --abbrev-commit//查看历史提交的commit id
f3d38d9 change at last2
ea6ce55 change at last
b020468 add 2
d2bc792 merge with no-ff
7e1479a add merge
c3e7db4 change all

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)
$ git tag v0.11 7e1479a //打标签

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)
$ git tag
v0.11
v1.0
v2.0

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git show v1.0 
commit f3d38d922b1ccd5dc9705997f5a30fc41679e089
Author: Dillenii <1264564847@qq.com>
Date: Wed Feb 15 16:31:22 2017 +0800

change at last2

diff --git a/hello.txt b/hello.txt
index 6ea752b..8f74ef9 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1,4 +1,4 @@
Hello World!
hello hello hello...
hello kitty :)
-stupid fool at 2
\ No newline at end of file
+stupid fool at 1
\ No newline at end of file

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git checkout dev
Switched to branch 'dev'

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)
$ git tag -a v2.0 -m "version 2.0"//创建带有说明的标签,用-a指定标签名,-m指定说明文字:可以看到说明文字

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)
$ git show v2.0
tag v2.0
Tagger: Dillenii <1264564847@qq.com>
Date: Wed Feb 15 17:12:36 2017 +0800

version 2.0

commit b020468bb2bbab8399fe2cf9b01bf0bb2a9b177b
Author: Dillenii <1264564847@qq.com>
Date: Wed Feb 15 15:31:16 2017 +0800

add 2

diff --git a/hello.txt b/hello.txt
index f2c71c4..6ea752b 100644
--- a/hello.txt
+++ b/hello.txt
@@ -1,4 +1,4 @@
Hello World!
hello hello hello...
hello kitty :)
-stupid fool at dev
\ No newline at end of file
+stupid fool at 2

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)
$ git tag //查看tag
v0.11
v1.0
v2.0

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)
$ git tag -d v0.11 v1.0 //删除tag v0.11 v1.0
Deleted tag 'v0.11' (was 7e1479a)
Deleted tag 'v1.0' (was f3d38d9)

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)
$ git push origin v2.0 //推送v2.0标签到远程
Counting objects: 29, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (21/21), done.
Writing objects: 100% (29/29), 2.41 KiB | 0 bytes/s, done.
Total 29 (delta 9), reused 0 (delta 0)
remote: Resolving deltas: 100% (9/9), done.
To https://github.com/Dillenii/learnGit.git
* [new tag] v2.0 -> v2.0//推送成功

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)
$ git push origin :refs/tags/v2.0 //删除远程的v2.0
To https://github.com/Dillenii/learnGit.git
- [deleted] v2.0 //删除成功


//远程库
$ ssh-keygen -t rsa -C "youremail@example.com
//a. 创建SSH Key,所在的文件夹会出现两个文件;
//b.登陆GitHub,打开“settings”,“SSH and GPS Keys”,“New SSH Key”,title随便填,key粘贴id_rsa.pub里面的内容
//这样你在电脑就可以推送到github上的仓库了。
//c.在GitHub上创建仓库:“New repository”,点击创建按钮;

//d. 推送内容
$ git remote add origin git@github.com:Dillenii/learnGit.git
//或者$ git remote add origin https://github.com/Dillenii/learnGit.git
//下一步,把本地库的所有内容推送到远程库上:

$ git push -u origin master
//由于远程库是空的,我们第一次推送master分支时,加上了-u参数,Git不但会把本地的master分支内容推送的远程新的master分支,还会把本地的master分支和远程的master分支关联起来,在以后的推送或者拉取时就可以简化命令。

//以上为向远程库推送,以下为向本地推送
$ git clone git@github.com:Dillenii/learnGit.git 

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)
$ git remote//查看远程库的信息
origin

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)
$ git remote -v//查看更详细的信息;如果没有推送权限,就看不到push的地址
origin https://github.com/Dillenii/learnGit.git (fetch)
origin https://github.com/Dillenii/learnGit.git (push)

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)
$ git push origin dev//向remote推送dev分支
Counting objects: 28, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (20/20), done.
Writing objects: 100% (28/28), 2.29 KiB | 0 bytes/s, done.
Total 28 (delta 9), reused 0 (delta 0)
remote: Resolving deltas: 100% (9/9), done.
To https://github.com/Dillenii/learnGit.git
* [new branch] dev -> dev

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git checkout -b dev origin/dev//在本地创建和远程分支对应的分支
Branch dev set up to track remote branch dev from origin.
Switched to a new branch 'dev'

//修改hello.txt

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)
$ git add hello.txt

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)
$ git commit -m "change origin branch dev"
[dev 710d49e] change origin branch dev
1 file changed, 1 insertion(+), 1 deletion(-)

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)
$ git push origin dev //再推送
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 313 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/Dillenii/learnGit.git
b020468..710d49e dev -> dev

//若其他人也向remote推送,信息冲突

$ git pull//从远程抓取分支,然后在本地合并,解决冲突,再推送;

//如果git pull提示“no tracking information”,则说明本地分支和远程分支的链接关系没有创建,用命令git branch --set-upstream branch-name origin/branch-name

 

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)
$ git branch -r //查看远程上的分支
origin/dev
origin/master
origin/origin/dev

 

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)
$ git push origin :dev //删除远程上的分支
To https://github.com/Dillenii/learnGit.git
- [deleted] dev

//自定义git

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (master)
$ git config --global alias.co checkout//配置别名

 

//最后帮助命令

kf@kf-PC MINGW64 ~/Desktop/htdocs/learnGit (dev)
$ git help
usage: git [--version] [--help] [-C <path>] [-c name=value]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
clone   Clone a repository into a new directory
init    Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
add     Add file contents to the index
mv      Move or rename a file, a directory, or a symlink
reset   Reset current HEAD to the specified state
rm      Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
bisect  Use binary search to find the commit that introduced a bug
grep    Print lines matching a pattern
log     Show commit logs
show    Show various types of objects
status  Show the working tree status

grow, mark and tweak your common history
branch  List, create, or delete branches
checkout Switch branches or restore working tree files
commit  Record changes to the repository
diff    Show changes between commits, commit and working tree, etc
merge   Join two or more development histories together
rebase  Reapply commits on top of another base tip
tag     Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
fetch  Download objects and refs from another repository
pull   Fetch from and integrate with another repository or a local branch
push   Update remote refs along with associated objects

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.

完。。。

 

posted @ 2017-02-13 11:21  米娜-火箭  阅读(337)  评论(5编辑  收藏  举报