一:Git简述
Git是一个分布式的版本控制工具,简单易用。
Git全局配置:
git config --global user.name “用户名" # 配置使用Git的用户名
git config --global user.email “使用者邮箱" # 配置使用GIt的邮箱
git config --global color.ui true #配置颜色提示为打开状态
git config -l # 查看当前所有的git配置
二:Git的操作:
1. git init 工作区目录名 #使用该命令可以创建并初始化一个工作区,以目录的形式出现
在工作区的根目录下,有一个.git目录(版本库,或本地仓库)
2. git add 添加到暂存区的内容
使用该命令可以将工作区中的改动添加到暂存区。
3. git commit -m "提交信息"
将暂存区中的内容提交到本地某个分支中
注意:在工作区中修改的内容不能直接commit提交,但可以使用git commit -a -m “提交信息” 一个命令既添加到暂存区,紧接着提交到本地仓库分支上,但不建议这样使用
4. git log
该命令用来查看提交日志,可以使用 git log --preety=oneline 的方式显示剪短的提交日志
5.git status
操作git的状态
6.git reset
git reset的作用是重置提交id(commit id)。
该命令有两个常用的参数:
--soft:重置HEAD游标的指向,但并不修改暂存区和工作区的内容。(将本地某个分支中的内容退回到暂存区和工作区)
lzs@ubuntu:~/mygit$ git reset --soft HEAD~1
lzs@ubuntu:~/mygit$ git log
commit 751fcfbb36fe1edd30aae49bc41e7f4f8c4e7719
Author: lzs <124379685@qq.com>
Date: Mon May 28 05:13:49 2018 -0700
给hi写了一句话
commit c6fc0499603eeb83760a2ea77b349d4b7c79397a
Author: lzs <124379685@qq.com>
Date: Mon May 28 05:12:37 2018 -0700
创建一个hi.txt文件
commit d0e7c0c4a98adb816155350b54f34c6663d2c5b3
Author: lzs <124379685@qq.com>
Date: Mon May 28 05:11:00 2018 -0700
给hello写了一句话
commit 7a26c5f1d5a82dfa7cba5a4df1990fcd2ab871fe
Author: lzs <124379685@qq.com>
Date: Mon May 28 05:08:31 2018 -0700
添加hello文件
lzs@ubuntu:~/mygit$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: test.txt
lzs@ubuntu:~/mygit$ git add .
lzs@ubuntu:~/mygit$ git commit -m "我重新来过"
[master a6e76e9] 我重新来过
1 file changed, 1 insertion(+)
create mode 100644 test.txt
lzs@ubuntu:~/mygit$ git log
commit a6e76e9c72ce8c9f9cd9c972213ccfa61ce34861
Author: lzs <124379685@qq.com>
Date: Mon May 28 05:23:49 2018 -0700
我重新来过
commit 751fcfbb36fe1edd30aae49bc41e7f4f8c4e7719
Author: lzs <124379685@qq.com>
Date: Mon May 28 05:13:49 2018 -0700
给hi写了一句话
commit c6fc0499603eeb83760a2ea77b349d4b7c79397a
Author: lzs <124379685@qq.com>
Date: Mon May 28 05:12:37 2018 -0700
创建一个hi.txt文件
commit d0e7c0c4a98adb816155350b54f34c6663d2c5b3
Author: lzs <124379685@qq.com>
Date: Mon May 28 05:11:00 2018 -0700
给hello写了一句话
commit 7a26c5f1d5a82dfa7cba5a4df1990fcd2ab871fe
Author: lzs <124379685@qq.com>
Date: Mon May 28 05:08:31 2018 -0700
添加hello文件
--hard:重置HEAD游标的指向,并且将暂存区和工作区的内容强制恢复到当前重置HEAD后游标的指向commit id状态。
lzs@ubuntu:~/mygit$ git log
commit a6e76e9c72ce8c9f9cd9c972213ccfa61ce34861
Author: lzs <124379685@qq.com>
Date: Mon May 28 05:23:49 2018 -0700
我重新来过
commit 751fcfbb36fe1edd30aae49bc41e7f4f8c4e7719
Author: lzs <124379685@qq.com>
Date: Mon May 28 05:13:49 2018 -0700
给hi写了一句话
commit c6fc0499603eeb83760a2ea77b349d4b7c79397a
Author: lzs <124379685@qq.com>
Date: Mon May 28 05:12:37 2018 -0700
创建一个hi.txt文件
commit d0e7c0c4a98adb816155350b54f34c6663d2c5b3
Author: lzs <124379685@qq.com>
Date: Mon May 28 05:11:00 2018 -0700
给hello写了一句话
commit 7a26c5f1d5a82dfa7cba5a4df1990fcd2ab871fe
Author: lzs <124379685@qq.com>
Date: Mon May 28 05:08:31 2018 -0700
添加hello文件
lzs@ubuntu:~/mygit$ git reset --hard d0e7c0c4a98adb816155350b54f34c6663d2c5b3
HEAD is now at d0e7c0c 给hello写了一句话
lzs@ubuntu:~/mygit$ git log
commit d0e7c0c4a98adb816155350b54f34c6663d2c5b3
Author: lzs <124379685@qq.com>
Date: Mon May 28 05:11:00 2018 -0700
给hello写了一句话
commit 7a26c5f1d5a82dfa7cba5a4df1990fcd2ab871fe
Author: lzs <124379685@qq.com>
Date: Mon May 28 05:08:31 2018 -0700
添加hello文件
例如
: git reset --soft HEAD~1 # 将当前HEAD游标的位置重置到上一次提交,但并不修改工作区与暂存区的内容。
git reset --hard 123456789(commit id) #将当前HEAD游标的位置重置到提交id 为123456789的状态,并强制工作区和暂存区的内容恢复到该提交id 的状态
特别注意:一旦使用git reset 的 --hard ,则工作区和暂存区的将强制恢复。如果后悔了,可以使用git reflog 来查看所有的提交(commit)和重置(reset)日志
#查看“上帝”日志
lzs@ubuntu:~/mygit$ git reflog
d0e7c0c HEAD@{0}: reset: moving to d0e7c0c4a98adb816155350b54f34c6663d2c5b3
a6e76e9 HEAD@{1}: commit: 我重新来过
751fcfb HEAD@{2}: reset: moving to HEAD~1
b8d3564 HEAD@{3}: commit: 创建了test文件
751fcfb HEAD@{4}: commit: 给hi写了一句话
c6fc049 HEAD@{5}: commit: 创建一个hi.txt文件
d0e7c0c HEAD@{6}: commit: 给hello写了一句话
7a26c5f HEAD@{7}: commit (initial): 添加hello文件
#恢复回去
lzs@ubuntu:~/mygit$ git reset --hard a6e76e9
HEAD is now at a6e76e9 我重新来过
lzs@ubuntu:~/mygit$ git log
commit a6e76e9c72ce8c9f9cd9c972213ccfa61ce34861
Author: lzs <124379685@qq.com>
Date: Mon May 28 05:23:49 2018 -0700
我重新来过
commit 751fcfbb36fe1edd30aae49bc41e7f4f8c4e7719
Author: lzs <124379685@qq.com>
Date: Mon May 28 05:13:49 2018 -0700
给hi写了一句话
commit c6fc0499603eeb83760a2ea77b349d4b7c79397a
Author: lzs <124379685@qq.com>
Date: Mon May 28 05:12:37 2018 -0700
创建一个hi.txt文件
commit d0e7c0c4a98adb816155350b54f34c6663d2c5b3
Author: lzs <124379685@qq.com>
Date: Mon May 28 05:11:00 2018 -0700
给hello写了一句话
commit 7a26c5f1d5a82dfa7cba5a4df1990fcd2ab871fe
Author: lzs <124379685@qq.com>
Date: Mon May 28 05:08:31 2018 -0700
添加hello文件
三:GitHub
GitHub是一个代码托管网站。
https://github.com/lijiuchangxin/mypython.git
1.在本地操作系统上使用命令创建秘钥。
ssh-keygen -t rsa -C "邮箱地址"。
2.寻找id_rsa.pub文件里存放的“公钥”,将其复制在GitHub上自己的账号里(SSH keys)里。
3.在GitHub上创建一个远程仓库,选中”initialize this repository whit a readme“复选框。
4.在本地进行git clone “远程仓库的地址”操作,在本地创建一个与远程仓库对应的仓库。
5.cd“克隆下来的目录(本地仓库)”
6.git remote add 远程主机名(习惯上origin) 远程仓库的ip # 将远程仓库与本地仓库进行关联
7.在本地仓库中进行开发(git add、 git commit)
8.git push -u origin master # 将本地仓库的修改推送到远程仓库
四:git分支管理
git branch 新建分支名 #创建新分支i
git branch #查看所有分支
git checkout 分支名 #切换分支
git checkout -b 新建分支名 #新建一个分之后,直接切换到该新建分支
git merge 合并分支名 #将某个分支的成果合并到当前分支
思考:git pull 与 git fetch的区别?如何故意制造冲突?