使用Git工作的一般流程

使用Git工作的一般流程

获得一个Git管理的工作区

使用Git开始工作,首先需要一个Git管理的工作区,这个工作去可以是自己init创建的,也可以是从远程仓库clone下来的。

自己初始化一个仓库

## 新建一个目录作为工作目录
$ mkdir git_demo   
$ cd git_demo
## 在本地初始化git仓库
$ git init
  • 1
  • 2
  • 3
  • 4
  • 5

这时,我们已经创建了一个本地仓库,但是,一般我们和其他人共同开发一个项目,则需要添加一个远程仓库。现在假设我已经才github上面建立了一个叫做git_demo的空仓库,现在需要将其添加到本地仓库。

## 添加一个叫origin的远程仓库
$ git remote add origin [email protected]:JavyZheng/git_demo.git
## 添加个README吧
$ vim README.md
$ git add README.md
$ git commit -m "first commit with README"
## 推送到远程仓库
$ git push -u origin master
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

到这里,就创建了一个可以推送到远程的git仓库。

从远程仓库获得工作目录

大多数时候,我们没有机会从头init的仓库,而是远程仓库已经存在,我们要参与到项目中,这时只需要将远程仓库clone下来就好。

## 把刚刚推送上去的仓库clone下来
$ git clone [email protected]:JavyZheng/git_demo.git
$ cd git_demo
$ git status
## 默认远程仓库名为origin
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

添加,修改,推送

不管是自己重新创建还是从远程仓库clone,我们现在得到了一个可以开展工作的工作区,这个工作区被git仓库所管理。

进行一些修改后,可以通过add, commit, push来推送到远程仓库

## 添加所有修改
$ git add .
## 提交修改
$ git commit -m "add some files"
## 推送到远程仓库
$ git push 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

分支

通常情况下,当我们需要添加一个新功能的时候,是不能在主分支上直接修改的,这时,需要创建一个新的分支,等功能开发并测试完成之后,再合并到主分支。

假设我们需要新建一个临时分支iss1来处理一个问题。

## 新建一个iss1分支
$ git branch iss1
## 切换到iss1分支
$ git checkout iss1
Switched to branch 'iss1'
## 查看分支,当前已经在iss1分支上面
$ git branch
* iss1
  master
## 在当前分支上进行一些修改
$ echo "file3" >> file3
## 添加并提交修改到本地
$ git add file3
$ git commit -m "add file3"
## 推送到远程,因为现在远程还没有iss1分支,所以需要set-upstream
## 这样,在远程仓库就有了iss1分支,之后可以直接push
$ git push --set-upstream origin iss1

## iss1解决后,把修改合并会master,并删除iss1分支
$ git checkout master
$ git merge iss1
$ git branch -d iss1
$ git push
## 删除远程分支
$ git push origin :iss1
To [email protected]:JavyZheng/git_demo.git
 - [deleted]         iss1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

拉取别人的提交

在我们完成了某项功能的修改,需要push的远程的时候,协作者可能已经提交了他们的修改,这时,我们需要先把最新的提交拉取下来,加入我们的修改,再重新提交上去。

扫描二维码关注公众号,回复: 4704307 查看本文章
$ git push
## push被驳回了,因为有其他人已经提交了更新
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to '[email protected]:JavyZheng/git_demo.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

## 拉取远程提交内容并合并到当前工作区
$ git pull
## 重新push到远程
$ git push
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

处理冲突

当拉取协作者的提交时,很可能不同开发者修改了同一个文件的同一部分,这时候,就会出现冲突,我们需要手动解决这些冲突,再重新提交上去。

$ git pull
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 5 (delta 2), reused 5 (delta 2), pack-reused 0
Unpacking objects: 100% (5/5), done.
From github.com:JavyZheng/git_demo
   7d4f14a..e2e17d3  master     -> origin/master
## 尝试自动合并file1
Auto-merging file1
## 发现冲突,需要手动解决冲突
CONFLICT (content): Merge conflict in file1
Automatic merge failed; fix conflicts and then commit the result.

## 此时,git已经把可能冲突的地方都写进了文件
$ vim file1
## 可以看见冲突的地方
<<<<<<< HEAD
file1 + add 1
=======
file1 + del 4
>>>>>>> e2e17d311ec33700e94ce5dd694aa340920deb7c

## vim里手动解决冲突后,add进来
$ git add file1
$ git commit -m "resolve confict in file1"
## 推送到远程分支
$ git push

猜你喜欢

转载自blog.csdn.net/hzxOnlineOk/article/details/78775929