1.远程拉取gitlab 工程分支,并在本地建立分支

具体过程

  • 新建一个空文件
  • 初始化 git init
  • 自己要与origin master建立连接(下划线远程仓库链接) git remote add origin http://192.168.9.10:8888/root/game-of-life.git
  • 把远程分支拉到本地(game-of-live-first_branch为远程仓库的分支名) git fetch origin game-of-live-first_branch
  • 在本地创建分支game-of-live-first_branch并切换到该分支 git checkout -b game-of-live-first_branch origin/game-of-live-first_branch
  • 把game-of-live-first_branch远程分支上的内容都拉取到本地 git pull origin game-of-live-first_branch

2.修改分支的内容并上传给远程分支

3.如果想在linux中拉取远程分支代码

[root@localhost rollBack]# git init Initialized empty Git repository in /test/rollBack/.git/ [root@localhost rollBack]# git remote add origin git@192.168.9.10:root/game-of-life.git [root@localhost rollBack]# git fetch origin game-of-live-first_branch remote: Enumerating objects: 1770, done. remote: Counting objects: 100% (1770/1770), done. remote: Compressing objects: 100% (582/582), done. remote: Total 1770 (delta 1112), reused 1770 (delta 1112) Receiving objects: 100% (1770/1770), 15.00 MiB | 28.20 MiB/s, done. Resolving deltas: 100% (1112/1112), done. From 192.168.9.10:root/game-of-life

  • branch game-of-live-first_branch -> FETCH_HEAD [root@localhost rollBack]# git pull origin game-of-live-first_branch From 192.168.9.10:root/game-of-life
  • branch game-of-live-first_branch -> FETCH_HEAD

4.git使用

4.1.源码安装git [root@localhost ~]# yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel [root@localhost ~]# yum -y install perl-ExtUtils-MakeMaker package [root@localhost ~]# tar xvf git-2.9.5 -C /usr/src/ [root@localhost ~]# cd /usr/src/git-2.9.5 [root@localhost git-2.9.5]# make configure GIT_VERSION = 2.9.5 GEN configure [root@localhost git-2.9.5]# ./configure --prefix=/usr/local/ [root@localhost git-2.9.5]# make && make install


4.2.配置git

[root@localhost git-2.9.5]# git config --global user.name "yunjisuan" #配置git使用用户 [root@localhost git-2.9.5]# git config --global user.email "102110504@qq.com" #配置git使用邮箱 [root@localhost git-2.9.5]# git config --global color.ui true #语法高亮 [root@localhost git-2.9.5]# git config --list 查看全局配置 user.name=yunjisuan user.email=102110504@qq.com color.ui=true

查看生成的配置文件并添加新模块 #在root下

[root@localhost ~] # cat .gitconfig [user] name = clsn email = admin@znix.top [color] ui = true

[root@localhost ~]# cat .gitconfig [user] name = git email = 102110504@qq.com [color] ui = true [recevice] #添加此模块 denyCurrentBranch = ignore

获得配置命令的手册 #三种方法

git help <verb> git <verb> --help man git-<verb>


4.3.配置ssh,增加rsa认证

修改配置文件,增加下面内容 [root@localhost ~]# vi /etc/ssh/sshd_config RSAAuthentication yes PubkeyAuthentication yes

重启 [root@localhost ~]# systemctl restart sshd


4.4.获取git仓库(初始化仓库)

[root@localhost ~]# mkdir /git_data #创建目录 [root@localhost ~]# cd /git_data/ [root@localhost git_data]# git init #初始化目录 Initialized empty Git repository in /git_data/.git/ [root@localhost git_data]# git status #查看工作区状态 On branch master Initial commit nothing to commit (create/copy files and use "git add" to track)

添加并提交文件 [root@localhost git]# touch README [root@localhost git]# echo "please read first" >> README [root@localhost git]# git add * [root@localhost git]# git commit -m "first commit"


5.git仓库的管理

5.1向git内增加文件:

创建文件 [root@localhost git_data]# touch README [root@localhost git_data]# ls README [root@localhost 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) [root@localhost git_data]# git add ./* #添加文件跟踪 [root@localhost git_data]# git status On branch master Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: README [root@localhost git_data]# tree .git .git ├── branches ├── config ├── description ├── HEAD ├── hooks │ ├── applypatch-msg.sample │ ├── commit-msg.sample │ ├── post-update.sample │ ├── pre-applypatch.sample │ ├── pre-commit.sample │ ├── prepare-commit-msg.sample │ ├── pre-push.sample │ ├── pre-rebase.sample │ └── update.sample ├── index ├── info │ └── exclude ├── objects │ ├── e6 │ │ └── 9de29bb2d1d6434b8b29ae775ad8c2e48c5391 #查看隐藏目录下增加了一条文件 │ ├── info │ └── pack └── refs ├── heads └── tags 10 directories, 15 files [root@localhost git_data]# git commit -m 'first commit' #由工作区提交到本地仓库 [master (root-commit) 621be41] first commit 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 README [root@localhost git_data]# git status #查看git的状态 On branch master nothing to commit, working tree clean [root@localhost git_data]# tree .git/ #提交后的git目录状态 .git/ ├── branches ├── COMMIT_EDITMSG ├── config ├── description ├── HEAD ├── hooks │ ├── applypatch-msg.sample │ ├── commit-msg.sample │ ├── post-update.sample │ ├── pre-applypatch.sample │ ├── pre-commit.sample │ ├── prepare-commit-msg.sample │ ├── pre-push.sample │ ├── pre-rebase.sample │ └── update.sample ├── index ├── info │ └── exclude ├── logs │ ├── HEAD │ └── refs │ └── heads │ └── master ├── objects │ ├── 54 │ │ └── 3b9bebdc6bd5c4b22136034a95dd097a57d3dd │ ├── 62 │ │ └── 1be41481cd4dd65b58d1400173228125cdeb5b │ ├── e6 │ │ └── 9de29bb2d1d6434b8b29ae775ad8c2e48c5391 │ ├── info │ └── pack └── refs ├── heads │ └── master └── tags 15 directories, 21 files 总结说: git add * 添加到暂存区域 git commit 提交git仓库 -m 后面接上注释信息,内容关于本次提交的说明,方便自己或他人查看


5.2删除git内文件

1) 没有添加到暂存区的数据直接rm删除即可。 2) 已经添加到暂存区数据: git rm --cached database #→将文件从git暂存区域的追踪列表移除(并不会删除当前工作目录内的数据文件) git rm -f database #→将文件数据从git暂存区和工作目录一起删除


5.3改(重命名数据)

1)没有添加到暂存区的数据直接mv/rename改名即可。 2)已经添加到暂存区数据: git mv README NOTICE #这个改的时候工作目录里的文件名字也改了


5.4查 (查看历史记录) • 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 作者的修订时间


5.5还原历史数据

Git服务程序中有一个叫做HEAD的版本指针,当用户申请还原数据时,其实就是将HEAD指针指向到某个特定的提交版本,但是因为Git是分布式版本控制系统,为了避免历史记录冲突,故使用了SHA-1计算出十六进制的哈希字串来区分每个提交版本,另外默认的HEAD版本指针会指向到最近的一次提交版本记录,而上一个提交版本会叫HEAD^,上上一个版本则会叫做HEAD^^,当然一般会用HEAD~5来表示往上数第五个提交版本。 git reset --hard hash : • git reset --hard HEAD^ #→还原历史提交版本上一次 • git reset --hard 3de15d4 #→找到历史还原点的SHA-1值后,就可以还原(值不写全,系统会自动匹配)


5.6还原未来数据

什么是未来数据?就是你还原到历史数据了,但是你后悔了,想撤销更改,但是git log已经找不到这个版本了。 git reflog #→查看未来历史更新点 [root@localhost git_data]# git reflog b6a6e89 HEAD@{0}: reset: moving to HEAD^ 519bf0d HEAD@{1}: commit: third commit b6a6e89 HEAD@{2}: commit: second commit 621be41 HEAD@{3}: commit (initial): first commit [root@localhost git_data]# git reset --hard 519b #在删除third commit情况下返回third commit HEAD is now at 519bf0d third commit


5.7标签使用

前面回滚使用的是一串字符串,又长又难记。 git tag v1.0 #→当前提交内容打一个标签(方便快速回滚),每次提交都可以打个tag。 git tag #→查看当前所有的标签 git show v1.0 #→查看当前1.0版本的详细信息 git tag v1.2 -m "version 1.2 release is test" #→创建带有说明的标签,-a指定标签名字,-m指定说明文字 git tag -d v1.0 #→我们为同一个提交版本设置了两次标签,删除之前的v1.0 [root@localhost git_data]# git reset --hard HEAD^ #还原历史数据 HEAD is now at b6a6e89 second commit [root@localhost git_data]# git reset --hard v1.0 #利用标签回滚 HEAD is now at 519bf0d third commit


5.8对比数据

git diff可以对比当前文件与仓库已保存文件的区别,知道了对README作了什么修改后,再把它提交到仓库就放⼼多了。 git diff README [root@localhost git_data]# echo 222 >> README [root@localhost git_data]# git diff README diff --git a/README b/README index 2bbe845..56c43c1 100644 --- a/README +++ b/README @@ -1,2 +1,3 @@ 111 111 +222


6..分支结构

6.1 增加分支 git branch 分支名称


6.2分支切换 [root@localhost git_data]# git branch #查看分支 linux

  • master [root@localhost git_data]# git checkout linux #切换到分支 'linux' M README Switched to branch 'linux' [root@localhost git_data]# git branch
  • linux master

6.3在linux分支进行修改

例:就是切换到分支后进行普通操作

echo "clsn in linux" >> README


6.4合并代码

合并Linux上的代码到master上

git merge linux


6.5合并失败解决

模拟冲突,在文件的同一行做不同修改

在master 分支进行修改 [root@gitlab git_data]# cat README 2017年11月30日 [root@gitlab git_data]# echo "clsn in master">> README [root@gitlab git_data]# git commit -a -m "clsn 2017年11月30日 09点20分 " [master 7ab71d4] clsn 2017年11月30日 09点20分 1 file changed, 1 insertion(+)

切换到linux分支 [root@gitlab git_data]# git checkout linux [root@gitlab git_data]# cat README 2017年11月30日 [root@gitlab git_data]# echo "clsn in linux" >> README [root@gitlab git_data]# git commit -a -m "2017年11月30日 03" [linux 20f1a13] 2017年11月30日 03 1 file changed, 1 insertion(+)

回到master分区,进行合并,出现冲突 [root@gitlab git_data]# git checkout master 切换到分支 'master' [root@gitlab git_data]# git merge linux

冲突(内容):合并冲突于 README 自动合并失败,修正冲突然后提交修正的结果。

解决冲突 [root@gitlab git_data]# vim README 2017年11月30日 clsn in master clsn in linux #手工解决冲突 [root@gitlab git_data]# git commit -a -m "2017年11月30日 03" [master b6a097f] 2017年11月30日 03


6.6删除分支

因为之前已经合并了linux分支,所以现在看到它在列表中。

在这个列表中分支名字前没有 * 号的分支通常可以使用 git branch -d 删除掉;你已经将它们的工作整合到了另一个分支,所以并不会失去任何东西。 git branch --no-merged #查看所有包含未合并工作的分支

git branch -d 分支name


7.服务器命令总结

服务器上的管理命令

   mkdir:                                  //XX (创建一个空目录 XX指目录名)

  pwd: // 显示当前目录的路径。   git init //把当前的目录变成可以管理的git仓库,生成隐藏.git文件。   git add XX //把xx文件添加到暂存区去。   git commit –m “XX” //提交文件 –m 后面的是注释。   git status //查看仓库状态   git diff XX // 查看XX文件修改了那些内容   git log //查看历史记录   git reset --hard HEAD^ //或者 git reset --hard HEAD~ 回退到上一个版本(如果想回退到100个版本,使用git reset –hard HEAD~100 )   cat XX //查看XX文件内容   git reflog //查看历史记录的版本号id   git checkout -- XX //把XX文件在工作区的修改全部撤销。   git rm XX //删除XX文件   git checkout –b dev //创建dev分支 并切换到dev分支上   git branch //查看当前所有的分支   git checkout master // 切换回master分支   git merge dev //在当前的分支上合并dev分支   git branch –d dev //删除dev分支   git branch name //创建分支   git stash //把当前的工作隐藏起来 等以后恢复现场后继续工作   git stash list //查看所有被隐藏的文件列表   git stash apply //恢复被隐藏的文件,但是内容不删除   git stash drop //删除文件   git stash pop //恢复文件的同时 也删除文件   git remote //查看远程库的信息   git remote –v //查看远程库的详细信息    git remote add origin https://github.com/-- //关联一个远程库 --为路径   git push –u origin master //(第一次要用-u 以后不需要)把当前master分支推送到远程库   git push origin master //Git会把master分支推送到远程库对应的远程分支上   git clone https://github.com/--- // 从远程库中克隆 --为路径

   远程仓库相关命令
   检出仓库:       $ git clone 
   查看远程仓库:$ git remote -v
   添加远程仓库:$ git remote add 
   删除远程仓库:$ git remote rm 
   修改远程仓库:$ git remote set-url --push 
   拉取远程仓库:$ git pull 
   推送远程仓库:$ git push 
  *如果想把本地的某个分支test提交到远程仓库,并作为远程仓库的master分支,或者作为另外一个名叫test的分支,如下:
   $git push origin test:master         // 提交本地test分支作为远程的master分支
   $git push origin test:test              // 提交本地test分支作为远程的test分支