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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| git remote origin # 当你从远程仓库克隆时,实际上Git自动把本地的master分支和远程的master分支对应起来了,并且,远程仓库的默认名称是origin =========================================================================================== 多人协作的工作模式通常是这样:
1. 首先,可以试图用git push origin <branch-name>推送自己的修改; 2. 如果推送失败,则因为远程分支比你本地的内容更新,需要先用git pull试图合并; 3. 如果合并有冲突,则解决冲突,并在本地提交; 4. 没有冲突或者解决掉冲突后,再用git push origin <branch-name>推送就能成功! 5. 如果git pull提示no tracking information,则说明本地分支和远程分支的链接关系没有创建,用命令git branch --set-upstream-to <branch-name> origin/<branch-name>。 =========================================================================================== git remote -v origin [email protected]:ruopu89/test.git (fetch) origin [email protected]:ruopu89/test.git (push) # 用git remote -v显示更详细的信息 git push origin master Warning: Permanently added the RSA host key for IP address '13.250.177.223' to the list of known hosts. Counting objects: 18, done. Compressing objects: 100% (16/16), done. Writing objects: 100% (16/16), 1.38 KiB | 0 bytes/s, done. Total 16 (delta 7), reused 0 (delta 0) remote: Resolving deltas: 100% (7/7), completed with 1 local object. To [email protected]:ruopu89/test.git 83de0e9..5f4464a master -> master # 推送本地的master分支到远程的origin中,创建一个叫master的分支 git push origin dev Counting objects: 9, done. Compressing objects: 100% (5/5), done. Writing objects: 100% (7/7), 554 bytes | 0 bytes/s, done. Total 7 (delta 2), reused 0 (delta 0) remote: Resolving deltas: 100% (2/2), completed with 1 local object. remote: remote: Create a pull request for 'dev' on GitHub by visiting: remote: https://github.com/ruopu89/test/pull/new/dev remote: To [email protected]:ruopu89/test.git * [new branch] dev -> dev # 推送本地的dev分支到远程的origin上,创建一个叫dev的分支。这样远程就有了两个分支,一个master,一个dev mkdir learngit2 cd learngit2 git clone [email protected]:ruopu89/test.git Cloning into 'test'... Warning: Permanently added the RSA host key for IP address '13.229.188.59' to the list of known hosts. remote: Enumerating objects: 50, done. remote: Counting objects: 100% (50/50), done. remote: Compressing objects: 100% (25/25), done. remote: Total 50 (delta 17), reused 50 (delta 17), pack-reused 0 Receiving objects: 100% (50/50), done. Resolving deltas: 100% (17/17), done. cd test/ # 进入克隆下来的目录 ls -a . .. .git LICENSE readme.txt # 查看master分支中的文件 git branch * master # 查看现在的分支是master git checkout -b dev origin/dev Branch dev set up to track remote branch dev from origin. Switched to a new branch 'dev' # 创建远程origin的dev分支到本地 git branch * dev master ls hello.txt LICENSE readme.txt # dev分支中的文件 echo env > env.txt # 在dev分支中创建文件 git add env.txt git commit -m "add env" [dev afff0e7] add env 1 file changed, 1 insertion(+) create mode 100644 env.txt git push origin dev Counting objects: 4, done. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 329 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To [email protected]:ruopu89/test.git a285424..afff0e7 dev -> dev # 推送到远程的dev分支中 cd ../learngit # 到之前的目录 ls hello.txt LICENSE readme.txt # 这里并没有env文件 git branch * dev issue master # 现在也在dev分支上 cp ../learngit2/test/env.txt ./ # 将克隆下来的目录中的env.txt文件复制到当前目录中 ls env.txt hello.txt LICENSE readme.txt echo abc >> env.txt # 修改env.txt文件 git add env.txt git commit -m "add new env" [dev c997d7c] add new env 1 file changed, 2 insertions(+) create mode 100644 env.txt git push origin dev To [email protected]:ruopu89/test.git ! [rejected] dev -> dev (fetch first) error: failed to push some refs to '[email protected]:ruopu89/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. # 推送失败。因为刚才最新提交和你试图推送的提交有冲突,解决办法也很简单,Git已经提示我们,先用git pull把最新的提交从origin/dev抓下来,然后,在本地合并,解决冲突,再推送 git pull remote: Enumerating objects: 4, done. remote: Counting objects: 100% (4/4), done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done. From github.com:ruopu89/test a285424..afff0e7 dev -> origin/dev There is no tracking information for the current branch. Please specify which branch you want to merge with. See git-pull(1) for details
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> dev # git pull也失败了,有两种方法解决。一是git pull 远程地址/分支,二是git branch --set-upstream-to=origin/<branch> dev git pull [email protected]:ruopu89/test.git dev From github.com:ruopu89/test * branch dev -> FETCH_HEAD Auto-merging env.txt CONFLICT (add/add): Merge conflict in env.txt Automatic merge failed; fix conflicts and then commit the result. # 使用第一种方法pull下来文件 git branch --set-upstream-to=origin/dev dev Branch dev set up to track remote branch dev from origin. git pull U env.txt Pull is not possible because you have unmerged files. Please, fix them up in the work tree, and then use 'git add/rm <file>' as appropriate to mark resolution, or use 'git commit -a'. # 使用第二种方法pull文件,只是提示不能合并文件,因为上面已经拉过一次了 vim env.txt env abc git add env.txt git commit -m "fix env" [dev 59a0059] fix env git push origin dev Counting objects: 7, done. Compressing objects: 100% (3/3), done. Writing objects: 100% (4/4), 451 bytes | 0 bytes/s, done. Total 4 (delta 1), reused 0 (delta 0) remote: Resolving deltas: 100% (1/1), done. To [email protected]:ruopu89/test.git afff0e7..59a0059 dev -> dev # 修改文件后再添加、提交、推送就没问题了。
|