git
1、版本控制
1、协同修改
2、数据备份
3、权限控制
4、历史记录
5、分支管理
2、版本控制分类
集中式版本控制工具
SVN、CVS、VSS
分布式版本控制工具 避免单点故障
Git
3、Git优势
1、大部分操作在本地完成
2、完整性保证
3、尽可能添加数据而不是删除或者修改数据
4、分支操作非常快捷
5、与Linux命令前全面兼容
4、Git区间
工作区 -> git add -> 暂存区 -> git commit -> 本地库
写代码 临时存储 历史版本
5、Git和代码托管中心(维护远程库)
局域网环境下:
GitLab服务器
外网环境下:
Githuab
码云
5、本地库和远程库
团队内的协作
团队外的协作
6、本地库初始化
命令 git init
luoxu@lenovo:~/testGit$ git init Initialized empty Git repository in /home/luoxu/testGit/.git/ luoxu@lenovo:~/testGit$ ll -la total 12 drwxr-xr-x 3 luoxu luoxu 4096 Jul 31 10:12 ./ drwxr-xr-x 33 luoxu luoxu 4096 Jul 31 10:12 ../ drwxr-xr-x 7 luoxu luoxu 4096 Jul 31 10:12 .git/ luoxu@lenovo:~/testGit$ ll -la .git total 40 drwxr-xr-x 7 luoxu luoxu 4096 Jul 31 10:12 ./ drwxr-xr-x 3 luoxu luoxu 4096 Jul 31 10:12 ../ drwxr-xr-x 2 luoxu luoxu 4096 Jul 31 10:12 branches/ -rw-r--r-- 1 luoxu luoxu 92 Jul 31 10:12 config -rw-r--r-- 1 luoxu luoxu 73 Jul 31 10:12 description -rw-r--r-- 1 luoxu luoxu 23 Jul 31 10:12 HEAD drwxr-xr-x 2 luoxu luoxu 4096 Jul 31 10:12 hooks/ drwxr-xr-x 2 luoxu luoxu 4096 Jul 31 10:12 info/ drwxr-xr-x 4 luoxu luoxu 4096 Jul 31 10:12 objects/ drwxr-xr-x 4 luoxu luoxu 4096 Jul 31 10:12 refs/
注意:.git目录是存放本地库中相关子目录和文件,不要随意修改
7、设置签名
形式
用户名:luoxu
email: luoxu@qq.com
作用:用于区分不同开发人员的身份
辨析:这里设置的签名和登录远程库的账号密码没有关系
命令:
项目级别/仓库级别:仅仅对当前本地库范围内有效,优先级高
git config
git config user.name luoxu_pro //pro 代表项目级别
git config user.email luoxu_pro@qq.com
信息的保存的位置在 .git/config
luoxu@lenovo:~/testGit$ cat .git/config [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true [user] name = tom_pro email = tom_pro@qq.com
系统用户级别:登录当前操作系统用户有效,优先级低
git config --global
git config --global user.name luoxu_glb //glb 代表系统级别
git config --global user.email luoxu_glb@qq.com
信息的保存的位置在 ~/.git/config
luoxu@lenovo:~$ cat ~/.gitconfig [user] name = luoxu_glb email = luoxu_glb@qq.com
8、基本操作
8.1、查看工作区。暂存区的状态
git status
luoxu@lenovo:~/testGit$ vim good.txt luoxu@lenovo:~/testGit$ git status On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) good.txt nothing added to commit but untracked files present (use "git add" to track)
8. 2、将工作区 文件的新建或者修改添加到暂存区,在暂存区可以撤销
git add [filename]
luoxu@lenovo:~/testGit$ git add good.txt luoxu@lenovo:~/testGit$ git status On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: good.txt
8.3、将暂存区的内容提交到本地库,提交后不可以撤销
git commit -m "commint message" [filename]
luoxu@lenovo:~/testGit$ git commit -m "my second commit--modify good.txt" good.txt [master d5f76ea] my second commit--modify good.txt 1 file changed, 2 insertions(+), 1 deletion(-)
8.4、查看本地库的历史记录
git log //空格向下翻页
luoxu@lenovo:~/testGit$ git log commit d5f76ea74602550e5837045344e6d5b69523dc32 (HEAD -> master) Author: tom_pro <tom_pro@qq.com> Date: Wed Jul 31 14:29:03 2019 +0800 my second commit--modify good.txt commit 533e53d09f1aaa23d55dbe18d6791a40bb374641 Author: tom_pro <tom_pro@qq.com> Date: Wed Jul 31 14:16:17 2019 +0800
giit --pretty=oneline
luoxu@lenovo:~/testGit$ git log --pretty=oneline ad06cc96b359eb8348f132da85bb186733b014c7 (HEAD -> master) third change d5f76ea74602550e5837045344e6d5b69523dc32 my second commit--modify good.txt 533e53d09f1aaa23d55dbe18d6791a40bb374641 my first commit new file good.txt
giit --oneline
luoxu@lenovo:~/testGit$ git log --oneline ad06cc9 (HEAD -> master) third change d5f76ea my second commit--modify good.txt 533e53d my first commit new file good.txt
git reflog
luoxu@lenovo:~/testGit$ git reflog ad06cc9 (HEAD -> master) HEAD@{0}: commit: third change //head是我们进行版本控制的指针 ad06cc9索引值的一部分 d5f76ea HEAD@{1}: commit: my second commit--modify good.txt 533e53d HEAD@{2}: commit (initial): my first commit new file good.txt
8. 5、前进后退
本质是移动head指针
基于索引值操作【推荐】
git reset --hard 【索引值一部分】
luoxu@lenovo:~/testGit$ git reset --hard d5f76ea HEAD is now at d5f76ea my second commit--modify good.txt luoxu@lenovo:~/testGit$ git reflog d5f76ea (HEAD -> master) HEAD@{0}: reset: moving to d5f76ea ad06cc9 HEAD@{1}: commit: third change d5f76ea (HEAD -> master) HEAD@{2}: commit: my second commit--modify good.txt 533e53d HEAD@{3}: commit (initial): my first commit new file good.txt
使用^符号 只能后退 一个^退一步
git reset --hard head^^^ 往后退三步
使用^符号 只能后退 一个^退一步
git reset --hard head~3 往后退三步
git help 命令 查看帮助
git help reset
git reset --soft 仅仅移动本地库指针 Does not touch the index file暂存区 or the working tree at all
git reset --mixed 仅仅移动本地库和暂存区的指针 Resets the index but not the working tree
git reset --hard Resets the index and working tree. Any changes to tracked files in the working tree since <commit> are
discarded.
删除并找回的前提是:删除前,文件存在的状态提交到了本地库
方法:git reset --hard 【索引值一部分】
8.6、比较文件
git diff [filieName] 将工作区文件和暂存区进行比较
git diff [本地库的历史版本][filieName] 将工作区文件和本地库的历史版本进行比较
git diff 比较所有文件
luoxu@lenovo:~/testGit$ git diff good.txt //Git是以行为单位进行比较 diff --git a/good.txt b/good.txt index d9a1bc9..356e102 100644 --- a/good.txt +++ b/good.txt @@ -1,2 +1,3 @@ good morning 2nd changed luoxu +insert one line //+代表增加的行
9、分支管理
好处
A、并行开发
B、各个分支之间无影响
9.1分支操作
9.1.1、创建分支
git branch [分支名]
9.1.2、查看分支
git branch -v
9.1.3、切换分支
git checkout 【分支名】
9.1.4、合并分支
A、切换到要被合并的分支上 接受修改的分支 git merge [旧的分支]
B、git merge [有新内容的分支]
9.1.5、解决冲突
冲突的表现 冲突的解决
第一步:编辑文件,改动冲突的部分。保存
第二步:git add 【文件名】
第三步:git commit
A、修改 master分支内容,并提交到本地库 luoxu@lenovo:~/testGit$ git branch -v 查看分支 当前在master 分支下 hot_fix d5f76ea my second commit--modify good.txt * master d5f76ea my second commit--modify good.txt luoxu@lenovo:~/testGit$ vim good.txt 编辑文件 added by master luoxu@lenovo:~/testGit$ 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: good.txt no changes added to commit (use "git add" and/or "git commit -a") luoxu@lenovo:~/testGit$ git add good.txt luoxu@lenovo:~/testGit$ git commit -m "master" good.txt [master f8a94a2] master 1 file changed, 2 insertions(+), 1 deletion(-) B、修改 hot_fix 分支内容,并提交到本地库 luoxu@lenovo:~/testGit$ git checkout hot_fix 切换到hot_fix M good.txt Switched to branch 'hot_fix' luoxu@lenovo:~/testGit$ vim good.txt 编辑文件 added by hot_fix luoxu@lenovo:~/testGit$ git add good.txt luoxu@lenovo:~/testGit$ git status On branch hot_fix Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: good.txt luoxu@lenovo:~/testGit$ git commit -m "hot_fix" good.txt [hot_fix 6968745] hot_fix 1 file changed, 2 insertions(+) 3、 master合并hot_fix分支 luoxu@lenovo:~/testGit$ git merge hot_fix Auto-merging good.txt CONFLICT (content): Merge conflict in good.txt Automatic merge failed; fix conflicts and then commit the result. luoxu@lenovo:~/testGit$ more good.txt good morning <<<<<<< HEAD 2nd changed luoxu added by master ======= 2nd changed luoxu insert one lin added by hot_fix >>>>>>> hot_fix luoxu@lenovo:~/testGit$ git add good.txt luoxu@lenovo:~/testGit$ git commit -m "merged" good.txt 合并commit不能加分支名 fatal: cannot do a partial commit during a merge. luoxu@lenovo:~/testGit$ git commit -m "merged" [master e8e3f41] merged luoxu@lenovo:~/testGit$
Git用hash算法保证数据完整性
10、本地创建远程库和地址别名
luoxu@lenovo:~/testGit$ git remote -v luoxu@lenovo:~/testGit$ git remote add testGithub https://github.com/xuluo/test.git luoxu@lenovo:~/testGit$ git remote -v testGithub https://github.com/xuluo/test.git (fetch) testGithub https://github.com/xuluo/test.git (push) luoxu@lenovo:~/testGit$
11、推送
luoxu@lenovo:~/testGit/.git$ env | grep -i proxy 查看有没有设置代理 ALL_PROXY=socks://127.0.0.1:1080/ no_proxy=localhost,127.0.0.0/8,::1 NO_PROXY=localhost,127.0.0.0/8,::1 luoxu@lenovo:~/testGit/.git$ unset ALL_PROXY 取消代理 luoxu@lenovo:~/testGit/.git$ env | grep -i proxy no_proxy=localhost,127.0.0.0/8,::1 NO_PROXY=localhost,127.0.0.0/8,::1 成功push luoxu@lenovo:~/testGit/.git$ git push origin master Counting objects: 15, done. Delta compression using up to 4 threads. Compressing objects: 100% (7/7), done. Writing objects: 100% (15/15), 1.22 KiB | 1.22 MiB/s, done. Total 15 (delta 1), reused 0 (delta 0) remote: Resolving deltas: 100% (1/1), done. To https://github.com/xuluo/test.git * [new branch] master -> master
12、另一个用户克隆
切换到另一个用户的工作目录,将github上的项目文件克隆下来
git clone https://github.com/xuluo/test.git
作用: A 完整把远程库下载到本地
B、自动创建origin的远程地址别名
C、自动初始化本地库
另一个用户对文件做修改,推送之前,要加入团队成员才能推送到远程库
13、拉取 pull =fetch +merge
git fetch [远程地址别名] [远程分支名] fetch 后的内容默认在[远程地址别名/远程分支名]分支下,可以查看是否需要再merge
git merge [远程地址别名/远程分支名]
git pull 是直接fetch and merge 一起做
luoxu@lenovo:~/testGit$ ls good.txt luoxu@lenovo:~/testGit$ git fetch origin master 无法fetch ^Z [1]+ Stopped git fetch origin master luoxu@lenovo:~/testGit$ env | grep -i proxy 查看代理 ALL_PROXY=socks://127.0.0.1:1080/ no_proxy=localhost,127.0.0.0/8,::1 NO_PROXY=localhost,127.0.0.0/8,::1 all_proxy=socks://127.0.0.1:1080/ luoxu@lenovo:~/testGit$ unset ALL_PROXY 撤销代理服务器 luoxu@lenovo:~/testGit$ unset all_proxy luoxu@lenovo:~/testGit$ git fetch origin master remote: Enumerating objects: 4, done. remote: Counting objects: 100% (4/4), done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. From https://github.com/xuluo/test * branch master -> FETCH_HEAD e8e3f41..994490c master -> origin/master luoxu@lenovo:~/testGit$ git checkout origin/master 切换到origin/master查看fetch下来的内容 Note: checking out 'origin/master'. 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 994490c Create README.md luoxu@lenovo:~/testGit$ ls 确认无误后,多了一个readme.md 再切换到master目录下,进行merge good.txt README.md luoxu@lenovo:~/testGit$ git checkout master Previous HEAD position was 994490c Create README.md Switched to branch 'master' luoxu@lenovo:~/testGit$ ls master下面还只有一个good.txt文件 good.txt luoxu@lenovo:~/testGit$ git merge origin master merge: origin - not something we can merge luoxu@lenovo:~/testGit$ git merge origin/master 合并origin/master 到master分支 Updating e8e3f41..994490c Fast-forward README.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 README.md luoxu@lenovo:~/testGit$ ls good.txt README.md