Git的使用总结

Python 2.7
MacOS Sierra 10.12.1


前言

实习中的第一个小分享,刚好弥补了我想学git的一些需求,主要还是公司的gitlab代码管理相关,以后自己也可以上传github了


git的安装

采用HomeBrew安装,HomeBrew官网 http://brew.sh/

  1. 下载HomeBrew打开终端,输入
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

如报错提示如下

Error: /usr/local/Cellar is not writable.

则再次在终端输入

sudo chown -R $USER /usr/local
  1. 安装git,仍在原来的终端输入
didi@localhost:/$ brew install git

待安装后,输入如下命令即可确认完成安装

didi@localhost:/$ brew list
git lrzsz openssl thefuck wget

常用指令集合,本地仓库管理与更迭版本

首先创建一个仓库(文件夹),这里为gittest,然后初始化仓库

didi@localhost:~/Desktop/gittest$ git init
Initialized empty Git repository in /Users/didi/Desktop/gittest/.git/

此条命令会在本地桌面上的gittest文件夹中创建.git文件夹,默认不可见,用于管理版本,不用修改。

举个栗子

新建文件,以test.txt为例,可使用文本编辑器编辑建立,也可以使用vim命令,甚至python脚本来建立,文件格式不限对文件进行编辑,任意内容输入。然后对这个第一版本进行操作:

  • 第一步是用git add把文件添加进去,实际上就是把文件修改添加到暂存区;
  • 第二步是用git commit提交更改,实际上就是把暂存区的所有内容提交到当前分支,也就是版本库中。
  • 第三步是使用git log 对提交到版本库的文件进行查看

这样就完成了一个版本的创立提交,详细命令如下所示

didi@localhost:~/Desktop/gittest$ git add test.txt #从工作区提交到暂存区
didi@localhost:~/Desktop/gittest$ git commit -m "txtfile V1" #这是对版本进行提交,和sql中的commit差不多,txtfile V1是指提交时候理解为备注,最好是自己能够确认出这个版本的添加信息
[master f7444c8] txtfile V1
1 file changed, 1 insertion(+)

版本回滚

使用命令git reset –hard 版本号 ,假设已在原始的第一个版本的txt中重新添加了编辑,之后进行add和commit操作,现在版本库中就有两个版本的test.txt文件了,但是,从文件夹中看到的版本是最后HEAD指向的版本

didi@localhost:~/Desktop/gittest$ vim test.txt
didi@localhost:~/Desktop/gittest$ git add test.txt
didi@localhost:~/Desktop/gittest$ git commit -m "txtfile V2"
didi@localhost:~/Desktop/gittest$ git log
commit 28e6401eaf9c2121e9d2c48732770e698787f8b1
Author: didi <shenyucong@didichuxing.com>
Date:   Fri Dec 9 16:59:50 2016 +0800
    txtfile V2

commit d8649ea01e9bc5118ee3e8eb361aae92e1403151
Author: didi <shenyucong@didichuxing.com>
Date:   Fri Dec 9 16:22:35 2016 +0800
    txtfile V1

didi@localhost:~/Desktop/gittest$ git reset --hard d864

如果找不到原来的位置了,使用git reflog来查看

文件删除

工作区的文件删除后,但还未删除版本库中的文件,可以使用git checkout – filename命令进行恢复文件到工作区

didi@localhost:~/Desktop/gittest$ ls
test.txt
didi@localhost:~/Desktop/gittest$ rm test.txt
didi@localhost:~/Desktop/gittest$ ls
didi@localhost:~/Desktop/gittest$ git status
On branch dev
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: test.txt
no changes added to commit (use "git add" and/or "git commit -a")
didi@localhost:~/Desktop/gittest$ git checkout -- test.txt
didi@localhost:~/Desktop/gittest$ ls
test.txt

版本库中文件删除

如果要真的删除数据,需要将版本库中的一并删除,使用命令git rm filename然后再从commit

didi@localhost:~/Desktop/gittest$ git rm testpy.py
rm 'testpy.py'
didi@localhost:~/Desktop/gittest$ git commit -m "del testpy.py"
[dev e313c41] testpy.py
1 file changed, 1 deletion(-)
delete mode 100644 testpy.py

版本merge

版本之间的merge,在主线master进行merge合并,会提示两条线路之间差异,主线merge后可删除支线

didi@localhost:~/Desktop/gittest$ git checkout master
Switched to branch 'master'

didi@localhost:~/Desktop/gittest$ ls
test.txt testpy.py
didi@localhost:~/Desktop/gittest$ git checkout dev
Switched to branch 'dev'

didi@localhost:~/Desktop/gittest$ ls
test.txt
didi@localhost:~/Desktop/gittest$ git checkout master
Switched to branch 'master'

didi@localhost:~/Desktop/gittest$ git merge dev
Updating 28e6401..e313c41
Fast-forward
testpy.py | 1 -
1 file changed, 1 deletion(-)
delete mode 100644 testpy.py

didi@localhost:~/Desktop/gittest$ ls
test.txt

支线删除

如果在dev这条支线上再新建一条新功能支线feature作为新加入的需求,但是在最后关头,feature支线以commit,还未merge时,需求不再需要,那么删除支线则需要使用-D而不是-d


didi@localhost:~/Desktop/gittest$ git checkout dev
Switched to branch 'dev'
didi@localhost:~/Desktop/gittest$ git branch -d feature
error: The branch 'feature' is not fully merged.
If you are sure you want to delete it, run 'git branch -D feature'.
didi@localhost:~/Desktop/gittest$ git branch -D feature
Deleted branch feature (was c126095).
didi@localhost:~/Desktop/gittest$ git branch
* dev
master

Stash保存工作台

工作未完成跳转分支,保存工作台
如果还有任务在一个分支上还未完成,且没有提交,需要紧急在另一个支线上修复bug,合并入master主线,则需要使用stash命令,将这一支线的状态保存下来,待日后进行处理,不然将无法回到主线上。


didi@localhost:~/Desktop/gittest$ vi test.txt
didi@localhost:~/Desktop/gittest$ git add test.txt
didi@localhost:~/Desktop/gittest$ git status
On branch dev
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified: test.txt

didi@localhost:~/Desktop/gittest$ git checkout master
error: Your local changes to the following files would be overwritten by checkout:
test.txt
Please commit your changes or stash them before you switch branches.
Aborting
didi@localhost:~/Desktop/gittest$ git stash
Saved working directory and index state WIP on dev: 41ee99f unfinshwork
HEAD is now at 41ee99f unfinshwork
didi@localhost:~/Desktop/gittest$ git checkout master
Switched to branch 'master'

在主线上另开一条支线专门用来修复bug,之后合并merge主线,删除修复bug的那条支线,之后切回原来的工作台支线,使用git stash pop命令恢复工作台,恢复的同时把stash内容也删除了.值得注意的是,当没有使用stash恢复时候,工作台上并没有状态,需要将stash保存的内容恢复回来才会有。

didi@localhost:~/Desktop/gittest$ git checkout dev
Switched to branch 'dev'
didi@localhost:~/Desktop/gittest$ git status
On branch dev
nothing to commit, working tree clean
didi@localhost:~/Desktop/gittest$ git stash list
stash@{0}: WIP on dev: 41ee99f unfinshwork
didi@localhost:~/Desktop/gittest$ git stash pop
On branch dev
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.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (254dd1c1d5ebcee38672435b082a58c3b732fde7)
didi@localhost:~/Desktop/gittest$ git status
On branch dev
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.txt

no changes added to commit (use "git add" and/or "git commit -a")


GitLab的使用

这里简单介绍下如何push自己的project到gitlab上,如果是新的project,建议先在gitlab上新建一个project,然后clone下来,之后将自己的project文件全部复制到clone的文件夹中,之后执行

这里因为公司原因,切掉了自己的下载地址,不过内网啦,别人也访问不了,这里是下载了gitlab上自己的Doubancrawl工程

add和commit进行提交,然后执行push进行云端提交,这里示例一下,(用了win的git bash,mac的git可以在shell中操作,步骤代码相同)

这里写图片描述

更多使用请详见: http://blog.csdn.net/huaishu/article/details/50475175


Git 上传分支-协同工作

一般来说,一个project提供给别人clone的是一个master主线上的东西,如果再次上传支线,将不会被别人所clone下来,比如我在另一台机器push自己的分支,那么分支被
push上去之后,如果没有merge到master,那么分支不会被clone。如果需要clone其分支进行二次开发,需要使用语言git checkout -b LOCALBRANCH origin/EXISTBRANCH其中LOCALBRANCH是需要在本地进行开发的branch,而EXISTBRANCH是gitlab端存在的分支,即需要进行再二次开发的分支。
比如,我在gitlab上已有分支如图,其中一个master是主分支,而其余两个是分支,我现在要对newfeature这个分支进行再开发

image

需要执行的命令如下,包括clone其master分支,之后与远程newfeature建立连接,在本地执行需要二次开发的分支进行操作,之后push分支即可

这里写图片描述

执行之后的效果如下

image


Git Tips–自动补全

Git通过bash-completion软件包实现命令补齐,在Mac OS X下可以通过Homebrew进行安装。

didi@localhost:brew search completion
bash-completion
didi@localhost:brew install bash-completion

根据bash-completion安装过程中的提示,修改文件~/.bash_profile文件,并在其中加入如下内容,以便在终端加载时自动启用命令补齐。


if [ -f $(brew --prefix)/etc/bash_completion ]; then
  . $(brew --prefix)/etc/bash_completion
fi

或者使用git源码进行安装自动补全,之后重启终端即可自动补全git命令


didi@localhost:~$ git clone https://github.com/git/git.git
didi@localhost:~$ cp git/contrib/completion/git-completion.bash ~/.git-completion.bash
didi@localhost:~$ source ~/.git-completion.bash

Git Tips–图形化界面

详见:https://my.oschina.net/amstrong/blog/159114 总结不错,不再赘述

Git Tips–查看主线支线合并情况

image

使用命令git log –graph –pretty=oneline –abbrev-commit

didi@localhost:~/Desktop/gittest$ git log --graph --pretty=oneline --abbrev-commit
* 53eea08 submit
|\
| * 41ee99f unfinshwork
| * 15b3b98 dev change test
| * f702c04 testdevo
| * 191a958 rm stash.txt
| * 74b027c stash
| * 76e61c4 unchangefile
* | f2b569b fix the bug
* | c8e48c9 another
* | c4f7a47 another
* | 5a9f011 finish job
* | ecdde9e fix the bugs from issue101
|/

Git Tips–多端访问同一账户

比较实际的例子,比如我需要在另一台电脑上上传一个project到自己的gitlab上,那么需要这台电脑生成的ssh keys进行登记,在win上产生ssh key代码如下

ssh-keygen -t rsa -C "YOUR_EMAIL@YOUREMAIL.COM"

命令中的YOUR_EMAIL@YOUREMAIL.COM替换为你注册gitlab时用的Email地址。
按照提示,一直回车,然后ssh key生成,之后将生成的id_rsa.pub添加到gitlab上的ssh key即可
直接使用命令将id_rsa.pub文件里的内容复制到剪切板中的命令:

clip < ~/.ssh/id_rsa.pub

然后复制过去就可以了同理可以使用n多台电脑push工程到project,只需要提前添加ssh key即可


Git的fork别人的project,修改并提交自己仓库

注意要先fork自己的仓库,才能push

建议查看:http://www.cnblogs.com/muzinan110/p/5300600.html


remote: Counting objects: 5, done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 5 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (5/5), done.
didi@localhost:~/Desktop$ ls
GitLabTest               xxxxxxxx
expect5.45.tar.gz        login                    test.py
SecureCRT731             check                    gittest                  login_public.sh          滴滴实习相关
SecureCRT731.zip         expect                   hadoop_test              shell_test               员工入职指导手册
didi@localhost:~/Desktop$ cd xxxxxxxx/
didi@localhost:~/Desktop/xxxxxxxx$ ls
create_hive.sql eta_main.sh
didi@localhost:~/Desktop/xxxxxxxxt$ vi pushnewfeature.txt
didi@localhost:~/Desktop/xxxxxxxx$ ls
create_hive.sql    eta_main.sh        pushnewfeature.txt
didi@localhost:~/Desktop/xxxxxxxxt$ git add pushnewfeature.txt
didi@localhost:~/Desktop/xxxxxxxx$ git commit -m "add pushnewfeatue.txt"
[master f2d0938] add pushnewfeatue.txt
 1 file changed, 2 insertions(+)
 create mode 100644 pushnewfeature.txt
didi@localhost:~/Desktop/xxxxxxxx$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
nothing to commit, working tree clean
didi@localhost:~/Desktop/xxxxxxxx$ git push -u origin master
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 326 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
.........
Branch master set up to track remote branch master from origin.

Git构建功能性分支进行开发

  • Step 1:源仓库的构建

这一步通常由项目发起人(项目管理员)来操作,源仓库为op/Chanjet_Asset_Management,并初始化两个分支master和develop.

  • Step 2:开发者fork源仓库

源仓库建立以后,每个开发就可以去复制一份源仓库到自己的gitlab账号中,然后作为自己开发所用的仓库。

  • Step 3:把自己开发者仓库clone到本地
  • Step 4:构建功能分支进行开发

对于Step4,具体的栗子:

didi@localhost:~/Desktop/xxxxxxxx$ cat issue.txt
this is new feature
a new feature
another feature
change third
didi@localhost:~/Desktop/xxxxxxxx$ git status
On branch develop
nothing to commit, working tree clean
didi@localhost:~/Desktop/xxxxxxxx$ cat issue.txt
this is new feature
a new feature
another feature
change third
didi@localhost:~/Desktop/xxxxxxxx$ git checkout developissue
Switched to branch 'developissue'
didi@localhost:~/Desktop/xxxxxxxx$ vi issue.txt
didi@localhost:~/Desktop/xxxxxxxx$ git add .
didi@localhost:~/Desktop/xxxxxxxx$ git commit -m "change 4"
[developissue 8fbf26b] change 4
 1 file changed, 1 insertion(+)
didi@localhost:~/Desktop/xxxxxxxx$ git checkout develop
Switched to branch 'develop'
didi@localhost:~/Desktop/xxxxxxxx$ git merge --no-ff -m "merge developissue" developissue
Merge made by the 'recursive' strategy.
 issue.txt | 1 +
 1 file changed, 1 insertion(+)
didi@localhost:~/Desktop/xxxxxxxx$ cat issue.txt
this is new feature
a new feature
another feature
change third
change 4
didi@localhost:~/Desktop/xxxxxxxx$ git branch
* develop
  developissue
  master
didi@localhost:~/Desktop/xxxxxxxx$ git branch -d developissue
Deleted branch developissue (was 8fbf26b).
didi@localhost:~/Desktop/xxxxxxxx$ git branch
* develop
  master
didi@localhost:~/Desktop/xxxxxxxx$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
didi@localhost:~/Desktop/xxxxxxxxt$ git push -u origin develop
Counting objects: 16, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (14/14), done.
Writing objects: 100% (16/16), 1.46 KiB | 0 bytes/s, done.
Total 16 (delta 9), reused 0 (delta 0)
......
 * [new branch]      develop -> develop
Branch develop set up to track remote branch develop from origin.

之后就可以看到push上了一个develop分支

image

和master不一样的是,develop中多了一项issue.txt文件,这就完成了新功能得提交

  • Step 5:向管理员提交pull request

    在完成了“讨论”功能(当然,也可能对自己的develop进行了多次合并,完成了多个功能),经过测试以后,觉得没问题,就可以请求管理员把自己仓库的develop分支合并到源仓库的develop分支中

  • Step 6 管理员测试、合并

管理员登陆gitlab,看到了开发者对源仓库发起的pull request。
管理员需要做的事情就是
1. 对开发者的代码进行review。
2. 在他的本地测试新建一个测试分支,测试开发者的代码:

判断是否同意合并到源仓库的develop中,如果经过测试没问题,可以把开发者的代码合并到源仓库的develop中


最后

gitlab也好,github也好,最终只是个版本管理工具,现在我的使用只是当做高逼格版的百度云而已,也就是个仓库,或许以后进行代码协助开发的时候才会体会到gitlab的强大吧,科科~


PS:祝大家2017新年快乐!也祝自己在想要的道路上越走越远!大家共勉!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值