您的位置:首页 > 编程语言

简单使用git管理代码

2017-11-11 14:14 441 查看
一、进行初始化

Git global setup

git config –global user.name “tai”

git config –global user.email “taichong@yunify.com”

Create a new repository

git clone git@git.internal.yunify.com:chong/test.git

cd test

touch README.md

git add README.md

git commit -m “add README”

git push -u origin master

至此创建完成

————————————

二、编写简单代码并推到project

1、首先建立分支并切换到分支下

git branch

* master

git checkout -b test1

Switched to a new branch ‘test1’

git branch

master

* test1

2、下拉master的内容,进行编辑

git pull origin master

From git.internal.yunify.com:chong/test

* branch master -> FETCH_HEAD

Already up-to-date.

3、如果希望添加除了master上之外的文件需要

git add file_name

git commit file_name——》这时会出现一个vim形式的文档里面是你要添加的文件的一些记录信息,需要进去编写一个标题,作为这次提交的标题

4、上推这次提交

git push –set-upstream origin file_name

Counting objects: 3, done.

Delta compression using up to 4 threads.

Compressing objects: 100% (3/3), done.

Writing objects: 100% (3/3), 340 bytes | 0 bytes/s, done.

Total 3 (delta 0), reused 0 (delta 0)

remote:

remote: Create merge request for test1:

remote: https://×××××××××××××××××/chong/test/merge_requests/new?merge_request%5Bsource_branch%5D=test1 ——》发送这个连接给当前project的master,让他进行审核

remote:

To git@git.internal.yunify.com:chong/test.git

* [new branch] test1 -> test1

Branch test1 set up to track remote branch test1 from origin.

这时会发现branch变成了2.说明当前有两个分支。

至此,你的提交完成

————————————

三、当master merge commit之后,你会发现自己写的代码在master上出现了

四、删除远程和本地分支

当你的任务提交并merge之后,在确认确实没有必要留着分支的时候,需要删除自己建立的分支。一是为了节省空间,二是为了代码和界面整洁度。对于开源来说,整齐的代码才更有竞争力。

1、查看一下当前所有的分支,包括远程和本地

git branch -a

master

* test1

remotes/origin/master

remotes/origin/test1

2、确定自己要删除的分支,进行删除

(1)删除远程分支

在Git v1.7.0 之后,可以使用这种语法删除远程分支:

git push origin –delete BranchName

删除tag这么用:

git push origin –delete tag TagName

否则,可以使用这种语法,推送一个空分支到远程分支,其实就相当于删除远程分支:

git push origin :BranchName

这是删除tag的方法,推送一个空tag到远程tag:

git tag -d TagNamerefs/tags/

git push origin :refs/tags/TagName

演示:

删除远程:

git branch -a

master

* test1

remotes/origin/master

remotes/origin/test1

git push origin :test1

To git@git.internal.yunify.com:chong/test.git

- [deleted] test1

git branch -a

master

* test1

remotes/origin/master

(2)删除本地分支

git branch -d BranchName

注意,无法删除当前分支,需要先做切换

git branch -d test1

error: Cannot delete the branch ‘test1’ which you are currently on.

git branch

help

master

* test1

git checkout master ——》切换到master

Switched to branch ‘master’

Your branch is up-to-date with ‘origin/master’.

git branch -d test1

Deleted branch test1 (was 85b8935).

git branch -a

help

* master

remotes/origin/master

五、进阶篇

当你pull master之后,进行修改,直到你进行push操作之间,有其他人也在对master进行修改,而且,你们修改的很可能是同一个文件的时候,该怎么做呢?

If someone else made changes to the master branch while you were working on your branch, you could pull in those updates. ————来自github

假设你现在基于远程分支”origin”,创建一个叫”test2”的分支。

git checkout -b test2

创建两个提交

(1)第一个commit,注意,git commit –amend可以理解成基于上次commit继续追加

$ git add a.txt

git commit

[test2 c73a536] The first commit

1 file changed, 0 insertions(+), 0 deletions(-)

create mode 100644 a.txt

touch b.txt

git add b.txt

git commit –amend

[test2 40f3725] The first commit merge a b.txt

Date: Fri Nov 10 11:44:07 2017 +0800

2 files changed, 0 insertions(+), 0 deletions(-)

create mode 100644 a.txt

create mode 100644 b.txt

(2)第二次提交

touch c.txt

git add c.txt

git commit c.txt

[test2 b51d80a] The second commit
1 file changed, 0 insertions(+), 0 deletions(-)

create mode 100644 c.txt

但是与此同时,有些人也在”origin”分支上做了一些修改并且做了提交了. 这就意味着”origin”和”mywork”这两个分支各自”前进”了,它们之间”分叉”了。

在这里,你可以用”pull”命令把”origin”分支上的修改拉下来并且和你的修改合并; 结果看起来就像一个新的”合并的提交”(merge commit):

git pull origin master

From git.internal.yunify.com:chong/test

* branch master -> FETCH_HEAD

Merge made by the ‘recursive’ strategy.

d.txt | 0

1 file changed, 0 insertions(+), 0 deletions(-)

create mode 100644 d.txt

ls

a.txt b.txt c.txt d.txt hello.go README.md

git push –set-upstream origin test2

Counting objects: 6, done.

Delta compression using up to 4 threads.

Compressing objects: 100% (6/6), done.

Writing objects: 100% (6/6), 699 bytes | 0 bytes/s, done.

Total 6 (delta 2), reused 0 (delta 0)

remote:

remote: Create merge request for test2:

remote: https://××××××××××××/chong/test/merge_requests/new?merge_request%5Bsource_branch%5D=test2
remote:

To git@git.internal.yunify.com:chong/test.git

* [new branch] test2 -> test2

Branch test2 set up to track remote branch test2 from origin.

但是,这样操作,你会发现很多次的commit记录

commit 08578f476219b411f08feac42fa322a0e016a8a8

Merge: b51d80a e416a9f

Author: tai taichong@yunify.com

Date: Fri Nov 10 11:56:55 2017 +0800

Merge branch 'master' of git.internal.yunify.com:chong/test into test2


commit e416a9fe85c309486a254c76b1f6ba4152a77b4f

Merge: 621e593 b151b2e

Author: tai taichong@yunify.com

Date: Fri Nov 10 11:56:14 2017 +0800

Merge branch 'help' into 'master'

The third commit

See merge request !2


commit b151b2e12bdaed80724c8759b92d33ab0e1632f6

Author: tai taichong@yunify.com

Date: Fri Nov 10 11:53:15 2017 +0800

The third commit


commit b51d80a7a89efd88798d814d329f00096df05976

Author: tai taichong@yunify.com

Date: Fri Nov 10 11:47:59 2017 +0800

The second commit


commit 40f372526b2cf4b88b25deb65ad5cb67bde8a6e7

Author: tai taichong@yunify.com

Date: Fri Nov 10 11:44:07 2017 +0800

The first commit
merge a b.txt


【注意】:

目前commit信息很多,所以,我这里先清楚commit到只有两个文件的时候。

git rm a.txt b.txt c.txt d.txt ————》git add a.txt b.txt c.txt d.txt

————》git push origin master ————》git push -f

如果commit记录增长很多,看起来会很乱。所以为了减少review的工作量和复杂度,建议采用下面的方法:

http://blog.csdn.net/chenansic/article/details/44122107

如果想简单了解git运行方式,可以访问:

https://guides.github.com/activities/hello-world/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  git 编辑器