git版本控制工具学习笔记

学习资源来自github官网及廖雪峰教程
http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

git基本命令

git add file 提交文件
git commit -m "what changes" 提交更改信息注释

git status 查看修改状态

git log 查看日志

git log --pretty=oneline 一行显示日志信息

git reset --hard HEAD^ 版本回退到上一个版本

git reset --hard xxxx(某版本号前几位) 回退到xxxx版本

git reflog 用来记录你的每一次命令

暂存区概念:

把文件往Git版本库里添加的时候,是分两步执行的:

第一步是用git add把文件添加进去,实际上就是把文件修改添加到暂存区;

第二步是用git commit提交更改,实际上就是把暂存区的所有内容提交到当前分支。

git add 某文件
vim 某文件
git commit -m "add 某文件"

实际上提交的是原来的某文件,并非当前工作目录中的某文件
可以再次:

git add 某文件
git commit -m "add 某文件"进行提交新的某文件
git checkout -- file 放弃工作区的最近一次修改
git reset HEAD file 把暂存区的修改撤销掉


git rm file 删除file文件
git commit -m "remove file"

远程仓库

1、创建SSH Key

$ ssh-keygen -t rsa -C "[email protected]"

可以在用户主目录里找到.ssh目录,里面有id_rsaid_rsa.pub两个文件。
这两个就是SSH Key的秘钥对,id_rsa是私钥,不能泄露出去,id_rsa.pub是公钥,可以放心地告诉任何人。

2、登陆GitHub,打开“Account settings”,“SSH Keys”页面, 然后,点“Add SSH Key”。
填上任意Title,在Key文本框里粘贴id_rsa.pub文件的内容:

新建一个repository
本地 learngit目录下执行

git remote add origin https://github.com/lesliefish/learngit.git

把本地内容推送至远程库:

yu@ubuntu:~/Linux/223/learngit$ git push -u origin master
Username for 'https://github.com': leslie
Password for 'https://[email protected]':

把本地库的内容推送到远程,用git push命令,实际上是把当前分支master推送到远程。

推送最新修改

yu@ubuntu:~/Linux/223/learngit$ git push origin master
Username for 'https://github.com': lesliefish
Password for 'https://[email protected]':
Everything up-to-date

每次本地提交后,只要有必要,就可以使用命令git push origin master推送最新修改;

远程库克隆到本地

git clone https://github.com/lesliefish/gitskills

分支

创建分支dev

yu@ubuntu:~/Linux/223/learngit$ git checkout -b dev
Switched to a new branch 'dev'
yu@ubuntu:~/Linux/223/learngit$ git branch
* dev
  master

修改readme.txt后提交

yu@ubuntu:~/Linux/223/learngit$ vim readme.txt
yu@ubuntu:~/Linux/223/learngit$ git add readme.txt
yu@ubuntu:~/Linux/223/learngit$ git commit -m "branch test"
[dev af18f3f] branch test
 1 file changed, 1 insertion(+)

此时修改的readme.txt只是分支上的readme.txt
切换回master分支发现本里readme.txt内容并未改变

yu@ubuntu:~/Linux/223/learngit$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
yu@ubuntu:~/Linux/223/learngit$ cat readme.txt

合并分支到master上git merge dev

yu@ubuntu:~/Linux/223/learngit$ git merge dev
Updating ee8f6f3..af18f3f
Fast-forward
 readme.txt | 1 +
 1 file changed, 1 insertion(+)

删除分支,删除后查看分支,发现只有master分支了

yu@ubuntu:~/Linux/223/learngit$ git branch -d dev
Deleted branch dev (was af18f3f).
yu@ubuntu:~/Linux/223/learngit$ git branch
* master

bug分支
Git还提供了一个stash功能,可以把当前工作现场“储藏”起来,等以后恢复现场后继续工作

多人协作
查看远程库的信息,用git remote

yu@ubuntu:~/Linux/223/learngit$ git remote
origin
yu@ubuntu:~/Linux/223/learngit$ git remote -v
origin  https://github.com/lesliefish/learngit.git (fetch)
origin  https://github.com/lesliefish/learngit.git (push)

标签

yu@ubuntu:~/Linux/223/learngit$ git tag v1.0
yu@ubuntu:~/Linux/223/learngit$ git tag
v1.0

找到历史提交的commit id

yu@ubuntu:~/Linux/223/learngit$ git log --pretty=oneline --abbrev-commit
af18f3f branch test
ee8f6f3 new gitskills lib
b42f322 remove test.txt
7d35956 add test.txt
0995552 2ci tijiao readme.txt
3521dac git tracks changes
3421d8b how stage works
142ea61 append GPL
2a3df4e add distributed in readme.txt
17c026f new file readme.txt

add test.txt这次提交打标签:

yu@ubuntu:~/Linux/223/learngit$ git tag v0.8 7d35956
yu@ubuntu:~/Linux/223/learngit$ git tag
v0.8
v1.0

查看标签git show

yu@ubuntu:~/Linux/223/learngit$ git show v0.8
commit 7d3595652ac527edcfe561ae613fe286a39ad389
Author: lesliefish <yuleics@outlook.com>
Date:   Tue Feb 23 15:11:39 2016 +0800

    add test.txt

diff --git a/test.txt b/test.txt
new file mode 100644
index 0000000..95160f9
--- /dev/null
+++ b/test.txt
@@ -0,0 +1 @@
+wei he zhe me diao

删除本地标签

yu@ubuntu:~/Linux/223/learngit$ git tag -d v0.8
Deleted tag 'v0.8' (was 7d35956)

如果标签已经推送到远程,要删除远程标签就麻烦一点,先从本地删除:

git tag -d v0.9

然后远程删除:

git push origin :refs/tags/<tagname>

你可能感兴趣的:(git,版本控制工具)