项目管理工具——git(远程仓库的管理)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014104588/article/details/50616438

当从远程仓库clone时,git 自动把本地的master分支和远程的master分支对应起来。远程仓库的默认名称是origin
下面是查看远程仓库的信息

[jason@localhost git_test]$ git remote 
origin
[jason@localhost git_test]$ git remote -v
origin  git@github.com:jasonlinuxzhang/git_test.git (fetch)
origin  git@github.com:jasonlinuxzhang/git_test.git (push)

-v 是显示了可以抓取和推送的origin的地址。如果没有推送权限,就看不到push的地址。

[jason@localhost git_test]$ git push origin mmm
若远程仓库中没有mmm分支,则该命令会创建一个mmm分支,然后将本地mmm分支,push远程的mmm分支
一:从远程仓库克隆
当从远程仓库clone一份git库时
此时git branch显示如下

[jason@localhost git_test]$ git branch 
* master

项目组人员规定只能在dev分支上开发,所以要切换到dev分支,这里有下面两种方法:

1:
[jason@localhost git_test]$ git checkout dev
Branch dev set up to track remote branch dev from origin.
Switched to a new branch 'dev'
2:
[jason@localhost git_test]$ git checkout -b dev origin/dev
Branch dev set up to track remote branch dev from origin.
Switched to a new branch 'dev'

由显示的结果可以知道,这两个命令都可以切换到远程的dev分支。

二:提交更改
要将更改推送到远程仓库要先将更改add和commit到本地仓库,然后执行如下命令:

[jason@localhost git_test]$ git push origin dev

若此时远程仓库没有改变,仍然是你clone时候的那个版本,则此时push会成功。
若在你push之前,已经有人push了,将远程仓库dev分支上的内容更改了,则会出现错误,以为在远程自动合并出错,大致内容如下:

[jason@localhost git_test]$ git push origin dev
To [email protected]:jasonlinuxzhang/git_test.git
 ! [rejected]        dev -> dev (fetch first)
error: failed to push some refs to '[email protected]:jasonlinuxzhang/git_test.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 merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

此时需要将远程dev分支上的内容更新到本地,然后执行手动合并再提交:

[jason@localhost git_test]$ git fetch origin dev:temp
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 2), reused 3 (delta 2), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:jasonlinuxzhang/git_test
 * [new branch]      dev        -> temp

git fetch origin dev:temp命令是在本地创建一个与远程dev分支关联的temp分支,并将远程dev分支上的内容更新到dev分支。
然后git diff temp比较当前分支和temp分支的差别(可以省略)
然后执行git merge temp将temp分支和当前分支合并:

[jason@localhost git_test]$ git merge temp
Auto-merging remote_test
CONFLICT (content): Merge conflict in remote_test
Automatic merge failed; fix conflicts and then commit the result.

remote_test合并提示冲突,查看该文件:

this is just remote test

for test add new line


now chanage
haha i change it two!


10: test right
10:test_left

<<<<<<< HEAD
test2
=======
test1
>>>>>>> temp
~                

现在更改该文件再提交(git add git commit git push),就能成功了,此时远程版本库的内容是你修改冲突后推送的内容。
最后再删除temp分支

[jason@localhost git_test]$ git branch -D temp 
Deleted branch temp (was 1c93218).

-D是强制删除

此时对于别人来说,则远程库的版本又改变了,则可以通过如下命令来更新本地版本:

[jason@localhost git_test]$ git fetch origin dev
From github.com:jasonlinuxzhang/git_test
 * branch            dev        -> FETCH_HEAD
[jason@localhost git_test]$ git merge origin/dev
Updating 1c93218..006c253
Fast-forward
 remote_test | 1 +
 1 file changed, 1 insertion(+)

先将远程dev分支更新到本地,然后再合并到本地分支。

猜你喜欢

转载自blog.csdn.net/u014104588/article/details/50616438