1、git fetch 相当于是从远程获取最新到本地,不会自动merge,如下指令:
git fetch orgin master //将远程仓库的master分支下载到本地当前branch中 git log -p master ..origin/master //比较本地的master分支和origin/master分支的差别 git merge origin/master //进行合并
也可以用以下指令:
git fetch origin master:tmp //从远程仓库master分支获取最新,在本地建立tmp分支 git diff tmp //將當前分支和tmp進行對比 git merge tmp //合并tmp分支到当前分支
2. git pull:相当于是从远程获取最新版本并merge到本地
git pull origin master
git pull 相当于从远程获取最新版本并merge到本地
在实际使用中,git fetch更安全一些
1.1 创建git数据仓库
[root@gitlab ~]# mkdir git_data [root@gitlab ~]# ll total 4 -rw-------. 1 root root 1347 Mar 12 11:09 anaconda-ks.cfg drwxr-xr-x 2 root root 6 Mar 20 20:40 git_data
1.2 初始化git目录
[root@gitlab git_data]# git init Initialized empty Git repository in /root/git_data/.git/
1.3 查看git当前工作状态
[root@gitlab git_data]# git status # On branch master # # Initial commit # nothing to commit (create/copy files and use "git add" to track)
1.4 创建数据-提交数据
[root@gitlab git_data]# touch README [root@gitlab git_data]# ll total 0 -rw-r--r-- 1 root root 0 Mar 20 20:47 README [root@gitlab git_data]# git status # On branch master # # Initial commit # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # README nothing added to commit but untracked files present (use "git add" to track)
1.5 把文件上传的暂存区
[root@gitlab git_data]# git add README [root@gitlab git_data]# git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: README
1.6 把暂存区的文件提交的版本库,-m是对文件的说明信息
[root@gitlab git_data]# git commit -m ‘jiangboyang‘ [master (root-commit) bb1dc0b] jiangboyang 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 README
1.7 如果修改了已经上传到仓库的文件后,可以一步上传到仓库
说明:这里的一步上传到仓库是指已经存在仓库中的文件,修改后,支持这样的操作
[root@gitlab git_data]# echo daya >>test [root@gitlab git_data]# 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: test # no changes added to commit (use "git add" and/or "git commit -a") [root@gitlab git_data]# git commit -a -m "xiugaiwenjian" [master 3bd1ed4] xiugaiwenjian 1 file changed, 1 insertion(+) [root@gitlab git_data]# git status # On branch master nothing to commit, working directory clean
1.8 删除暂存区的数据
[root@gitlab git_data]# git reset HEAD test2 [root@gitlab git_data]# git rm --cached test rm ‘test‘ [root@gitlab git_data]# git rm -f test2 删除暂存区文件并且删除源文件 rm ‘test2‘ [root@gitlab git_data]# ll total 4 -rw-r--r-- 1 root root 5 Mar 20 20:16 test
1.9 重命名暂存区文件
[root@gitlab git_data]# git mv test test.txt [root@gitlab git_data]# ll total 0 -rw-r--r-- 1 root root 0 Mar 20 20:47 README -rw-r--r-- 1 root root 0 Mar 20 21:01 test.txt [root@gitlab git_data]# git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: test.txt
1.10 查看历史记录
查看历史提交记录
[root@gitlab git_data]# git log commit bb1dc0bd080f1c2de3fc42a1d3e4a6e16d716422 Author: dy <15555513217@163.com> Date: Tue Mar 20 20:51:34 2018 +0800
jiangboyang
1.11 查看最近几条提交记录
[root@gitlab git_data]# git log -3 commit bb1dc0bd080f1c2de3fc42a1d3e4a6e16d716422 Author: dy <15555513217@163.com> Date: Tue Mar 20 20:51:34 2018 +0800
jiangboyang
git log #→查看提交历史记录
git log -2 #→查看最近几条记录
git log -p -1 #→-p显示每次提交的内容差异,例如仅查看最近一次差异
git log --stat -2 #→--stat简要显示数据增改行数,这样能够看到提交中修改过的内容,对文件添加或移动的行数,并在最后列出所有增减行的概要信息
git log --pretty=oneline #→--pretty根据不同的格式展示提交的历史信息
git log --pretty=fuller -2 #→以更详细的模式输出提交的历史记录
git log --pretty=fomat:"%h %cn" #→查看当前所有提交记录的简短SHA-1哈希字串与提交者的姓名,其他格式见备注。
#→还可以使用format参数来指定具体的输出格式,这样非常便于后期编程的提取分析哦,常用的格式有:
格式 |
说明 |
%s |
提交说明。 |
%cd |
提交日期。 |
%an |
作者的名字。 |
%cn |
提交者的姓名。 |
%ce |
提交者的电子邮件。 |
%H |
提交对象的完整SHA-1哈希字串。 |
%h |
提交对象的简短SHA-1哈希字串。 |
%T |
树对象的完整SHA-1哈希字串。 |
%t |
树对象的简短SHA-1哈希字串。 |
%P |
父对象的完整SHA-1哈希字串。 |
%p |
父对象的简短SHA-1哈希字串。 |
%ad |
作者的修订时间。 |
1.12 还原未来数据
什么是未来数据?就是还原到历史数据了,但是你后悔了,想撤销更改,但是git log找不到这个版本了
git reflog ---查看未来历史的更新点
1.13 还原历史数据
[root@gitlab git_data]# git log 利用git log查看版本号 commit 3bd1ed4424eb5e66cfc5ce855228a3547e5bef47 Author: dy <15555513217@163.com> Date: Tue Mar 20 20:16:46 2018 +0800 xiugaiwenjian commit 90a7e9ce4469fda8712c0a9cdb39693bd34b92c5 Author: dy <15555513217@163.com> Date: Tue Mar 20 20:10:09 2018 +0800 ceshiwenjian_2018.3.21 [root@gitlab git_data]# ll total 4 -rw-r--r-- 1 root root 5 Mar 20 20:16 test [root@gitlab git_data]# git reset --hard 90a7e9ce HEAD is now at 90a7e9c ceshiwenjian_2018.3.21 [root@gitlab git_data]# cat test [root@gitlab git_data]# git reset --hard 3bd1ed4424e HEAD is now at 3bd1ed4 xiugaiwenjian [root@gitlab git_data]# cat test daya
1.14 标签使用
[root@gitlab git_data]# git tag v200217 给当前提交内容打一个标签,每次提交都可以打tag [root@gitlab git_data]# git tag 查看当前所有标签 v200217 [root@gitlab git_data]# git show v200217 查看当前标签详细信息 commit 5f499e63e4db0ce040e0527467a085fe644519b1 Author: dy <15555513217@163.com> Date: Wed Mar 21 13:19:56 2018 +0800 xiugai diff --git a/jiang.txt b/jiang.txt index ba9c1ad..c5e58fc 100644 --- a/jiang.txt +++ b/jiang.txt @@ -1 +1,2 @@ nihao +888 [root@gitlab git_data]# git reset --hard 8c44f2f HEAD is now at 8c44f2f 777 [root@gitlab git_data]# cat jiang.txt nihao [root@gitlab git_data]# git reset --hard v200217 HEAD is now at 5f499e6 xiugai [root@gitlab git_data]# cat jiang.txt nihao 888
1.15 对比数据
diff命令可以对比当前文件与仓库已保存文件的区别,从而知道对文件做了哪些修改
[root@gitlab git_data]# cat jiang.txt nihao 888 [root@gitlab git_data]# echo 666 >>jiang.txt [root@gitlab git_data]# git diff jiang.txt diff --git a/jiang.txt b/jiang.txt index c5e58fc..a232fb5 100644 --- a/jiang.txt +++ b/jiang.txt @@ -1,2 +1,3 @@ nihao 888 +666 [root@gitlab git_data]#
第2章 分支结构
[root@gitlab git_data]# git branch 查看当前系统所有分支 * master [root@gitlab git_data]# git branch linux 创建分支 [root@gitlab git_data]# git branch linux * master [root@gitlab git_data]# git checkout linux 切换分支 Switched to branch ‘linux‘ [root@gitlab git_data]# git branch * linux master [root@gitlab git_data]# git branch -D linux 删除分支 Deleted branch linux (was 5f499e6).
2.1 代码合并:
[root@gitlab git_data]# git merge linux Updating 90a7e9c..84a289b Fast-forward test | 1 + 1 file changed, 1 insertion(+)
2.1.1 合并分支时的冲突问题:
[root@gitlab git_data]# git branch linux * master [root@gitlab git_data]# cat test linux branch [root@gitlab git_data]# echo nihao >>test [root@gitlab git_data]# git commit -a -m "nihao" [root@gitlab git_data]# git checkout linux Switched to branch ‘linux‘ [root@gitlab git_data]# echo buhao >>test [root@gitlab git_data]# git commit -a -m "buhao" [linux c21fd3c] buhao 1 file changed, 1 insertion(+) [root@gitlab git_data]# git status # On branch linux nothing to commit, working directory clean [root@gitlab git_data]# git branch * linux master [root@gitlab git_data]# git checkout master Switched to branch ‘master‘ [root@gitlab git_data]# git merge linux Auto-merging test CONFLICT (content): Merge conflict in test Automatic merge failed; fix conflicts and then commit the result. [root@gitlab git_data]# vim test 手动解决冲突,只保留想要保留的数据然后保存 [root@gitlab git_data]# git commit -a -m "hebingshibai" [master e5092ee] hebingshibai [root@gitlab git_data]# cat test linux branch nihao
第3章 windows客户端使用
windows 上git软件网站 https://git-for-windows.github.io
软件下载地址:https://github.com/git-for-windows/git/releases/download/v2.15.1.windows.2/Git-2.15.1.2-64-bit.exe 软件安装默认即可。
第4章 搭建gitlab私有仓库
4.1 安装gitlab,软件比较大,注意内容空间
yum -y localinstall gitlab-ce-9.1.4-ce.0.el7.x86_64.rpm gitlab-ctl reconfigure #→初始化,就执行一次 gitlab-ctl status
4.2 在浏览器中输入ip地址进行访问,我这里ip是10.0.0.63
1.1 定义项目名称
1.1 创建完成后会提示没有ssh密钥:
在服务端生成密钥对,复制公钥内容粘贴到网页上即可
[root@gitlab ~]# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory ‘/root/.ssh‘. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: 84:76:c5:d0:b0:7a:28:b0:e2:0e:12:7c:d7:cf:4d:a4 root@gitlab The key‘s randomart image is: +--[ RSA 2048]----+ | o=. | | . oo | | . o + . | |. o ..= o | |o.....o.SE . | |.o. .. .o o | |o. o . | |+ | | . | +-----------------+ [root@gitlab ~]# cd /root/.ssh/ [root@gitlab .ssh]# cat id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAGOae1O+UBTUPJNIIgOTdgB0KXT26HhZgh5JFRgau6BifEI34goNMYxNQS5pHiSO6GdHbk+wSi5ZB3Xl9nWYL29zbtSC7TDWEoPlz/FCbk4LXylFF+20MXt0hu+NsBS8xkMk0uyIt4ELEfZ8KO/Ki2zT6aFUJrqmkqxnn9hQyoiOPZv0ewQEYHfgUnXlGkA21arIOL3fMuaLoGcuyeiTEbL2H60nG8N3kC3B/4EcUs18P9rqAKv2A2tMsHoQyzfTRNSHHf1bWnc28oZ4KcQrdIfOQkLQCXMF6Vb9HWmJ01xCdwMiTbcGTQnkudr8bmeJitNnlqIqoZ2sCYsHf52gR root@gitlab
1.1 上传文件到gitlab私有仓库:
[root@gitlab ~]# cd 43team [root@gitlab 43team]# touch README.md [root@gitlab 43team]# git add README.md [root@gitlab 43team]# git commit -m "add README" [master (root-commit) 9429222] add README 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 README.md [root@gitlab 43team]# git push -u origin master Counting objects: 3, done. Writing objects: 100% (3/3), 208 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To git@10.0.0.63:root/43team.git * [new branch] master -> master Branch master set up to track remote branch master from origin.