github 配置
git clone,git push,git pull
git remote
git branch --set-upstream-to
远程仓库
远程仓库,我们可以自己搭建 git 服务器作为远程仓库,也可以用 github,gitee 等来进行托管
接下来以 github 为例
关于github的几点说明
- 勾选指定语言的
.gitignore
选项,如 python。 .gitignore
文件,忽略不需要要提交到仓库的文件,可用通配符。- 设置用户名与邮件地址
git config --global user.name/user.email
$ git config --global user.name "MonkeyJerry" $ git config --global user.email "example@126.com"
- 查看 git 配置
git config --global --list
这些内容存在~/.gitconfig 文件中$ git config --global --list user.email=example@126.com user.name=MonkeyJerry
$ cd ~ $ cat .gitconfig [user] email = example@126.com name = MonkeyJerry
- 创建 SSH Key
ssh-keygen -t rsa -C <email>
-C` 即 commentssh-keygen -t rsa -C "example@126.com"
- github 添加 key 公钥
在用户目录. ssh 中有 id_rsa(私钥) 和 id_rsa.pub(公钥)。
只有添加了 key,github 才认你,你才可以推送代码到 github 远程仓库。
如果有多台电脑,把公钥 key 都添加在 github 就可以了。 - github 创建仓库 repository,远程仓库名字一般取名为 origin(见名知意,换成其他的也行),默认分支为 master
克隆远程仓库 git clone
- git clone: 克隆远程仓库,并将本地的 master 分支和远程 origin 的 master 分支建立联系
在 github 新建一个远程仓库 gitTest,现在我们用 ssh 方式把它克隆到本地
$ git clone git@github.com:MonkeyJerry/gitTest.git
Cloning into 'gitTest'...
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 10 (delta 1), reused 5 (delta 0), pack-reused 0
Receiving objects: 100% (10/10), done.
Resolving deltas: 100% (1/1), done.
提交代码到远程仓库 git push
- git push: 推送本地库内容到远程仓库
修改文件后,我们推送一下试试
$ git status
On branch master
Your branch is up to date with 'origin/master'.
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: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
$ git add readme.txt
$ git commit -m 'modify readme'
[master 5a4cee8] modify readme
1 file changed, 1 insertion(+)
# 推送本地修改到远程仓库
$ git push
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 299 bytes | 299.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:MonkeyJerry/gitTest.git
e28ccb8..5a4cee8 master -> master
获取远程仓库最新代码 git pull
我们在推送代码时一般先获取远程最新代码git pull
后,在推送git push
自己的修改。因为我们在我们修改这段时间可能其他人已经提交了他的修改到远程仓库了。
# 不先获取最新代码直接push会拒绝
$ git push
To github.com:MonkeyJerry/gitTest.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@github.com:MonkeyJerry/gitTest.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最新代码
$ git pull
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:MonkeyJerry/gitTest
5a4cee8..03df76d master -> origin/master
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt
Automatic merge failed; fix conflicts and then commit the result.
git pull 冲突
pull 时冲突时很正常的,我们本地解决冲突再推送。
# 冲突前
<<<<<<< HEAD
this is readme file and this is txt file.
=======
this is readme file.
other people modified.
>>>>>>> 03df76d85599180fbd50c03cec6b064acb14f03f
# 解决冲突修改为
this is readme file and this is txt file.
other people modified.
# 查看解决冲突后文件状态
$ git status
On branch master
Your branch and 'origin/master' have diverged,
and have 1 and 1 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
$ git add readme.txt
$ git commit -m 'conflict fixed'
[master 71e55fe] conflict fixed
# 解决冲突后再次push
$ git push
Counting objects: 6, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (6/6), 646 bytes | 646.00 KiB/s, done.
Total 6 (delta 0), reused 0 (delta 0)
To github.com:MonkeyJerry/gitTest.git
03df76d..71e55fe master -> master
关联远程仓库 git remote
先git clone
的好处是能够直接管理本地仓库和远程仓库,如果是本地新建项目,未事先关联,我们就需要手动关联。
git remote add <name> <url>
: 为远程仓库的名字,远程仓库的 ulr(ssh/https)
如我在我的 github 创建了一个 gitTest 的仓库,url 为 git@github.com:MonkeyJerry/gitTest.git
$ git remote add origin git@github.com:MonkeyJerry/gitTest.git
git remote -v
: 查看本地仓库已关联的远程仓库
$ git remote -v
origin git@github.com:MonkeyJerry/gitTest.git (fetch)
origin git@github.com:MonkeyJerry/gitTest.git (push)
git remote rm
: 删除已关联的远程仓库
$ git remote rm origin
$ git remote -v
关联本地和远程分支
同样,我们本地创建的目录,在我们推送代码前先获取远程最新代码。然而关联了仓库还不够,当我们git pull
时,会提示设置分支关系
命令中指定分支关系
$ git pull
remote: Enumerating objects: 22, done.
remote: Counting objects: 100% (22/22), done.
remote: Compressing objects: 100% (16/16), done.
remote: Total 22 (delta 1), reused 14 (delta 0), pack-reused 0
Unpacking objects: 100% (22/22), done.
From github.com:MonkeyJerry/gitTest
* [new branch] master -> origin/master
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> master
# 在git pull中指定分支关系
$ git pull origin master
From github.com:MonkeyJerry/gitTest
* branch master -> FETCH_HEAD
设置分支关系
每次在命令中指定分支关系,很麻烦。直接设置好分支关系,下次就只要直接敲git pull
、git push
即可。
git branch --set-upstream-to=origin/<branch> master
: 将远程仓库 origin 的分支和本地 master 分支建立联系。
注:在设置分支关系前必须执行过一次 git fetch/pull origin master
$ git branch --set-upstream-to=origin/master master
Branch 'master' set up to track remote branch 'master' from 'origin'.
$ git push
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 335 bytes | 335.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:MonkeyJerry/gitTest.git
71e55fe..6acf525 master -> master
dev 分支
最好只在 dev 分支上干活呢,我们先创建一个 dev 分支。
然后将 dev 分支的代码推送到远程 dev 分支
$ git checkout -b dev
Switched to a new branch 'dev'
# 首次-u并指定分支关系
$ git push -u origin dev
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'dev' on GitHub by visiting:
remote: https://github.com/MonkeyJerry/gitTest/pull/new/dev
remote:
To github.com:MonkeyJerry/gitTest.git
* [new branch] dev -> dev
Branch 'dev' set up to track remote branch 'dev' from 'origin'.
# 设置分支关系
$ git branch --set-upstream-to=origin/dev dev
Branch 'dev' set up to track remote branch 'dev' from 'origin'.
# 在本地dev分支修改并提交远程dev分支
$ git push
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 320 bytes | 320.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:MonkeyJerry/gitTest.git
6acf525..c9c84ea dev -> dev
这样就完成了远程仓库代码的获取和本地修改的代码推送