github:推送至远程仓库

上一节我们,都是在本地进行管理,如何将本地的仓库推送到远程仓库呢:

在github上先建立一个仓库,创建README文件不要勾选,因为一旦创建就失去了和本地仓库的整合性。

创建完成后,出现下面的选项:
这里写图片描述
1. 如果你没有仓库,那么可以在本地创建一个,然后退送到网上。
2. 如果你有本地仓库,可以和这个整合。并给出了具体命令

将本地仓库推送到远程仓库

在网页上创建完仓库后不能做任何操作,不然会有版本操作的记录,将本地库推送的时候会发生错误。只能远端删除重建。

加入-u参数:将origin 仓库的master分支设置为本地仓库的当前分支的upstream(上游),添加这个参数后,本地仓库运行git pull时可直从origin仓库的master分支获取内容,不用添加其他参数再。

Administrator@YOUNG-PC MINGW64 ~/HelloWorld (master)
$ git remote add origin git@github.com:x695/helloworld.git
Administrator@YOUNG-PC MINGW64 ~/HelloWorld (master)
$ git branch
  feature-A
  feature-B
  feature-C
* master
Administrator@YOUNG-PC MINGW64 ~/HelloWorld (master)
$ git push -u origin master
Warning: Permanently added the RSA host key for IP address '192.30.252.131' to the list of known hosts.
To git@github.com:x695/helloworld.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@github.com:x695/helloworld.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.
Administrator@YOUNG-PC MINGW64 ~/HelloWorld (master)
#在远端删除仓库,进行重建
$ git remote add origin git@github.com:x695/helloworld.git
fatal: remote origin already exists.
Administrator@YOUNG-PC MINGW64 ~/HelloWorld (master)
$ git push -u origin master
Counting objects: 20, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (10/10), done.
Writing objects: 100% (20/20), 1.56 KiB | 0 bytes/s, done.
Total 20 (delta 4), reused 0 (delta 0)
To git@github.com:x695/helloworld.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.
Administrator@YOUNG-PC MINGW64 ~/HelloWorld (master)
$

将其他分支推送到远程仓库

  1. 本地创建一个feature-D分支
  2. 将这个分支推送到远程仓库

$ git checkout -b feature-D
Switched to a new branch 'feature-D'
Administrator@YOUNG-PC MINGW64 ~/HelloWorld (feature-D)
$ git push -u origin feature-D
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:x695/helloworld.git
 * [new branch]      feature-D -> feature-D
Branch feature-D set up to track remote branch feature-D from origin.
Administrator@YOUNG-PC MINGW64 ~/HelloWorld (feature-D)
$

从远程仓库获取别人的仓库并进行维护

获取master仓库

刚才我们本地创建了一个分支D并push到远程仓库,那么假如有另外一个开发者想维护这个分支,该如何实现:

  1. 另外一个同事新建一个本地仓库,git clone 这个远程仓库
$ cd
Administrator@YOUNG-PC MINGW64 ~
$ pwd
/c/Users/Administrator
Administrator@YOUNG-PC MINGW64 ~
$ mkdir clone_helloworld
Administrator@YOUNG-PC MINGW64 ~
$ cd clone_helloworld/
Administrator@YOUNG-PC MINGW64 ~/clone_helloworld
$ git clone git@github.com:x695/helloworld.git
Cloning into 'helloworld'...
remote: Counting objects: 20, done.
remote: Compressing objects: 100% (6/6), done.
Receiving objects: 100% (20/20), done.
Resolving deltas: 100% (4/4), done.
remote: Total 20 (delta 4), reused 20 (delta 4), pack-reused 0
Checking connectivity... done.
#拉取完成后,并没有进入到这个仓库,所以当期提示信息看不出是在master下
Administrator@YOUNG-PC MINGW64 ~/clone_helloworld
$ git branch
fatal: Not a git repository (or any of the parent directories): .git
Administrator@YOUNG-PC MINGW64 ~/clone_helloworld
$ ls
helloworld/
Administrator@YOUNG-PC MINGW64 ~/clone_helloworld
$ pwd
/c/Users/Administrator/clone_helloworld
#进入到刚才克隆的仓库,会提示在master分支下
$ cd helloworld/
Administrator@YOUNG-PC MINGW64 ~/clone_helloworld/helloworld (master)
$ ls
READM.md
Administrator@YOUNG-PC MINGW64 ~/clone_helloworld/helloworld (master)
$ ls -la
total 5
drwxr-xr-x 1 Administrator 197121  0 三月 12 06:40 ./
drwxr-xr-x 1 Administrator 197121  0 三月 12 06:39 ../
drwxr-xr-x 1 Administrator 197121  0 三月 12 06:40 .git/
-rw-r--r-- 1 Administrator 197121 74 三月 12 06:40 READM.md
Administrator@YOUNG-PC MINGW64 ~/clone_helloworld/helloworld (master)
$ git branch
* master
Administrator@YOUNG-PC MINGW64 ~/clone_helloworld/helloworld (master)
# 加入参数a能看到远程仓库的信息
$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/feature-D
  remotes/origin/master
Administrator@YOUNG-PC MINGW64 ~/clone_helloworld/helloworld (master)
$

获取分支仓库D

$ git checkout -b feature-D origin/feature-D
Branch feature-D set up to track remote branch feature-D from origin.
Switched to a new branch 'feature-D'
Administrator@YOUNG-PC MINGW64 ~/clone_helloworld/helloworld (feature-D)
$

维护本地分支D,并提交

$ vi READM.md
Administrator@YOUNG-PC MINGW64 ~/clone_helloworld/helloworld (feature-D)
$ cat READM.md
hello world !
#get some diff
        -feature-A
        -feature-B
        -feature-C
        -feature-D
Administrator@YOUNG-PC MINGW64 ~/clone_helloworld/helloworld (feature-D)
$ git diff
diff --git a/READM.md b/READM.md
index 8531ab0..996edda 100644
--- a/READM.md
+++ b/READM.md
@@ -4,3 +4,4 @@ hello world !
        -feature-A
        -feature-B
        -feature-C
+       -feature-D
Administrator@YOUNG-PC MINGW64 ~/clone_helloworld/helloworld (feature-D)
#直接提交,并推送到远程仓库
$ git commit -am "Add feature-D"
[feature-D a4c023b] Add feature-D
 1 file changed, 1 insertion(+)
Administrator@YOUNG-PC MINGW64 ~/clone_helloworld/helloworld (feature-D)
$ git push
warning: push.default is unset; its implicit value has changed in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the traditional behavior, use:
  git config --global push.default matching
To squelch this message and adopt the new behavior now, use:
  git config --global push.default simple
When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.
Since Git 2.0, Git defaults to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch.
See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 280 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To git@github.com:x695/helloworld.git
   1c6a091..a4c023b  feature-D -> feature-D
Administrator@YOUNG-PC MINGW64 ~/clone_helloworld/helloworld (feature-D)
$

将别人的更改的分支(即线上最新的分支)拉取到本地

#项目创始人重新切换到原来的仓库上,分支D上获取别的开发者最新的更改
$ cd HelloWorld/
Administrator@YOUNG-PC MINGW64 ~/HelloWorld (feature-D)
$ git pull origin feature-D
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:x695/helloworld
 * branch            feature-D  -> FETCH_HEAD
   1c6a091..a4c023b  feature-D  -> origin/feature-D
Updating 1c6a091..a4c023b
Fast-forward
 READM.md | 1 +
 1 file changed, 1 insertion(+)
Administrator@YOUNG-PC MINGW64 ~/HelloWorld (feature-D)
$ cat READM.md
hello world !
#get some diff
        -feature-A
        -feature-B
        -feature-C
        -feature-D
Administrator@YOUNG-PC MINGW64 ~/HelloWorld (feature-D)
$

学习资料

中文

progit,git学习者的圣经
git游戏闯关

英文

tyr git

### 如何将本地代码推送GitHub远程仓库 #### 关联本地仓库GitHub远程仓库 为了使本地仓库能够与GitHub上的远程仓库交互,需先建立两者之间的连接。通过命令`git remote add origin http://github.com/用户名/项目名.git`可实现此操作[^1]。 #### 准备推送文件 在准备向远程仓库推送之前,应确保所有待上传的更改已被加入暂存区并通过提交保存到了本地仓库中。这可以通过执行如下两条指令完成: - `git add .`用于添加当前目录及其子目录中的所有新文件和修改过的文件到暂存区。 - 提交这些变更到本地仓库则需要用到`git commit -m "描述"`的形式来记录此次更新的信息[^2]。 #### 同步远程仓库内容(当远程仓库不是空的时候) 如果目标远程分支已存在数据,则建议先运行`git pull --rebase origin 主分支名称`以拉取最新的改动并应用到本地工作副本之上,从而减少冲突发生的可能性。 #### 执行推送动作 最后一步就是实际地把本地版本历史发送给远端服务器了。对于初次设置链接的情况而言,应当采用带有-u参数形式的push命令——即`git push -u origin master`,它不仅会传送最新的一次commit,还会自动设定追踪关系以便日后简化后续的操作流程[^3]。 ```bash # 建立与远程仓库的关系 $ git remote add origin https://github.com/用户名/仓库名.git # 添加所有未跟踪或被修改后的文件进入索引阶段 $ git add . # 创建一个新的提交对象并将之附加于HEAD指针所指向的位置处 $ git commit -m '初始化提交' # 如果远程已有内容的话, 需要获取其最新状态并与自己的分支合并起来 $ git pull --rebase origin main # 将本地main分支的数据同步至origin对应的remote位置上去 $ git push -u origin main ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贤时间

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值