创建远程仓库

当你已经在本地创建了一个Git仓库后,又想在GitHub创建一个Git仓库,并且让这两个仓库进行远程同步,这样,GitHub上的仓库既可以作为备份,又可以让其他人通过该仓库来协作,真是一举多得。

首先,登陆GitHub,然后,在右上角找到“New repository”按钮,创建一个新的仓库:

下载代码
SSH git@github.com:Ningning-Li/git_training.gitSSH需要认证
HTTPS https://github.com/Ningning-Li/git_training.git

[root@greg02 gittraining]#git clone https://github.com/Ningning-Li/git_training.git
Cloning into 'git_training'...
warning: You appear to have cloned an empty repository.
[root@greg02 gittraining]#ls
git_training
[root@greg02 gittraining]#cd git_training/
[root@greg02 git_training]#ls
[root@greg02 git_training]#ls -a
. .. .git
[root@greg02 git_training]#vim REANDME
[root@greg02 git_training]#vim hello.py
[root@greg02 git_training]#ls
hello.py REANDME
[root@greg02 git_training]#git add .
[root@greg02 git_training]#git commit -m "first commit"
[master (root-commit) 14a2083] first commit
2 files changed, 3 insertions(+)
create mode 100644 REANDME
create mode 100644 hello.py
[root@greg02 git_training]#git remote add origin https://github.com/Ningning-Li/git_training.git
fatal: remote origin already exists.
[root@greg02 git_training]#git push -u origin master
Username for 'https://github.com': Ningning-Li
Password for 'https://Ningning-Li@github.com':
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 295 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To https://github.com/Ningning-Li/git_training.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.

当你再次修改还需要用户密码

[root@greg02 git_training]#vim hi.py
[root@greg02 git_training]#git add .
[root@greg02 git_training]#git commit -m "add hi.py"
[master 7a4a192] add hi.py
1 file changed, 2 insertions(+)
create mode 100644 hi.py
[root@greg02 git_training]#git push -u origin master
Username for 'https://github.com': Ningning-Li
Password for 'https://Ningning-Li@github.com':
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 315 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/Ningning-Li/git_training.git
14a2083..7a4a192 master -> master
Branch master set up to track remote branch master from origin.

创建分支

[root@greg02 git_training]#git checkout -b dev
M hi.py
Switched to a new branch 'dev'
[root@greg02 git_training]#git branch dev
fatal: A branch named 'dev' already exists.
[root@greg02 git_training]#git checkout dev
M hi.py
Already on 'dev'
[root@greg02 git_training]#git branch
* dev
master
[root@greg02 git_training]#touch readme.txt
[root@greg02 git_training]#vim readme.txt
[root@greg02 git_training]#git add readme.txt
[root@greg02 git_training]#git commit -m "branch test"
[dev 6d63a9a] branch test
1 file changed, 1 insertion(+)
create mode 100644 readme.txt
[root@greg02 git_training]#git checkout master
M hi.py
Switched to branch 'master'
[root@greg02 git_training]#ls
hello.py hi.py REANDME

合并分支

[root@greg02 git_training]#git merge dev
Updating 7a4a192..6d63a9a
Fast-forward
readme.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 readme.txt
[root@greg02 git_training]#ls
hello.py hi.py readme.txt REANDME

git merge命令用于合并指定分支到当前分支。合并后,再查看readme.txt的内容,就可以看到,和dev分支的最新提交是完全一样的。

注意到上面的Fast-forward信息,Git告诉我们,这次合并是“快进模式”,也就是直接把master指向dev的当前提交,所以合并速度非常快。

合并完成后,就可以放心地删除dev分支了:

[root@greg02 git_training]#git branch -d dev
Deleted branch dev (was 6d63a9a).
删除后,查看branch,就只剩下master分支了:
$ git branch
* master

分支冲突

准备新分支开发:
[root@greg02 git_training]#git checkout -b dev
M hi.py
Switched to a new branch 'dev'
[root@greg02 git_training]#vim readme.txt
[root@greg02 git_training]#git add readme.txt
[root@greg02 git_training]#git commit -m "from dev"
[dev b76c207] from dev
1 file changed, 2 insertions(+), 3 deletions(-)

切换到master分支:

[root@greg02 git_training]#git checkout master
M hi.py
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 4 commits.
(use "git push" to publish your local commits)
[root@greg02 git_training]#vim readme.txt
[root@greg02 git_training]#git add readme.txt
[root@greg02 git_training]#git commit -m "from master"
[master a50aa2c] from master
1 file changed, 4 insertions(+)
[root@greg02 git_training]#git merge dev
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt
Automatic merge failed; fix conflicts and then commit the result.
[root@greg02 git_training]#git status
# On branch master
# Your branch is ahead of 'origin/master' by 5 commits.
# (use "git push" to publish your local commits)
#
# You have unmerged paths.
# (fix conflicts and run "git commit")
#
# Unmerged paths:
# (use "git add <file>..." to mark resolution)
#
# both modified: readme.txt
#
# 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: hi.py
#

Git用<<<<<<<=======>>>>>>>标记出不同分支的内容,vim编辑删掉,再提交。

[root@greg02 git_training]#git add readme.txt
[root@greg02 git_training]#git commit -m "confilict fixed"
[master 46885a9] confilict fixed

debug

[root@greg02 git_training]#git checkout -b dev
M hi.py
Switched to a new branch 'dev'
[root@greg02 git_training]#vim dev.py
[root@greg02 git_training]#vim readme.tx
[root@greg02 git_training]#git status
# On branch dev
# 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: hi.py
# modified: readme.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# dev.py
no changes added to commit (use "git add" and/or "git commit -a")

现在,用git status查看工作区,就是干净的(除非有没有被Git管理的文件),因此可以放心地创建分支来修复bug。

首先确定要在哪个分支上修复bug,假定需要在master分支上修复,就从master创建临时分支:

[root@greg02 git_training]#git stash
Saved working directory and index state WIP on dev: 46885a9 confilict fixed
HEAD is now at 46885a9 confilict fixed
[root@greg02 git_training]#git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 7 commits.
(use "git push" to publish your local commits)
[root@greg02 git_training]#git add readme.txt
[root@greg02 git_training]#git checkout -b issue-101
Switched to a new branch 'issue-101'
[root@greg02 git_training]#git add readme.txt
[root@greg02 git_training]#git commit -m "fix bug 101"
# On branch issue-101
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# dev.py
nothing added to commit but untracked files present (use "git add" to track)
修复完成后,切换到master分支,并完成合并,最后删除issue-101分支:
[root@greg02 git_training]#git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 7 commits.
(use "git push" to publish your local commits)
[root@greg02 git_training]#git merge --no-ff -m "merged bug fixd" issue-101
Already up-to-date.
[root@greg02 git_training]#git branch -d issue-101
Deleted branch issue-101 (was 46885a9).
[root@greg02 git_training]#git checkout dev
Switched to branch 'dev'
[root@greg02 git_training]#git status
# On branch dev
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# dev.py
nothing added to commit but untracked files present (use "git add" to track)
工作区是干净的,刚才的工作现场存到哪去了?用git stash list命令看看:
[root@greg02 git_training]#git stash list
stash@{0}: WIP on dev: 46885a9 confilict fixed

工作现场还在,Git把stash内容存在某个地方了,但是需要恢复一下,有两个办法:

一是用git stash apply恢复,但是恢复后,stash内容并不删除,你需要用git stash drop来删除;

另一种方式是用git stash pop,恢复的同时把stash内容也删了:

[root@greg02 git_training]#git stash pop
# On branch dev
# 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: hi.py
# modified: readme.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# dev.py
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (71a6bc430e1275a6d951e40447db317bd2e7ecc9)
再用git stash list查看,就看不到任何stash内容了:
$ git stash list
你可以多次stash,恢复的时候,先用git stash list查看,然后恢复指定的stash,用命令:
$ git stash apply stash@{0}

推送分支

如果要推送其他分支,比如dev,就改成:

但是,并不是一定要把本地分支往远程推送,那么,哪些分支需要推送,哪些不需要呢?

  • master分支是主分支,因此要时刻与远程同步;

  • dev分支是开发分支,团队所有成员都需要在上面工作,所以也需要与远程同步;

  • bug分支只用于在本地修复bug,就没必要推到远程了,除非老板要看看你每周到底修复了几个bug;

  • feature分支是否推到远程,取决于你是否和你的小伙伴合作在上面开发

多人协作的工作模式通常是这样:

  1. 首先,可以试图用git push origin branch-name推送自己的修改;

  2. 如果推送失败,则因为远程分支比你的本地更新,需要先用git pull试图合并;

  3. 如果合并有冲突,则解决冲突,并在本地提交;

  4. 没有冲突或者解决掉冲突后,再用git push origin branch-name推送就能成功!

如果git pull提示“no tracking information”,则说明本地分支和远程分支的链接关系没有创建,用命令git branch --set-upstream branch-name origin/branch-name

 git init初始化
git add file.py 把代码放入git暂存区
git commit 从代码暂存区存入仓库
git status 查看当前的代码状态
git checkout把代码从暂存区回滚到工作区 rm file 本地删除
git add/rm file 提交到暂存区
git reset HEAD file 从暂存区回滚到工作区
git checkout --file 把工作区里操作撤销 下载代码
SSH git@github.com:Ningning-Li/git_training.git
HTTPS https://github.com/Ningning-Li/git_training.git 分支 git checkout -b branch_name 创建并切换分支
git checkout branch_name切换分支
git pull从远程更新代码到本地
git push 把本地代码推到远程
git merge branch_name合并分支 git stash 把当前工作环境临时保存 git stash apply 恢复之前保存的临时工作 git stash list 查看临时保存的列表 git stash drop 删除当前临时保存的环境备份 git stash pop 恢复并删除临时保存的备份

常用命令

github创建远程仓库的更多相关文章

  1. git在本地创建远程仓库

    类似的博文,在前面的帖子里面也提到过,当时讲述的是一个入门级别的.其URL是ssh://username@repo-host-address/repo-path这种格式. 今天再说说如何创建类似Git ...

  2. windows下使用git和github建立远程仓库

    转自(http://www.bubuko.com/infodetail-430228.html) 从昨天开始就在看git的使用,因为在Windows下很多命令行操作都比较坑爹,但是今天再走了无数弯路之 ...

  3. git的使用(包括创建远程仓库到上传代码到git的详细步骤以及git的一些常用命令)

    A创建远程仓库到上传代码到git 1)登陆或这注册git账号 https://github.com 2)创建远程仓库 3)打开终端输入命令 cd到你的本地项目根目录下,执行如下git命令 git in ...

  4. Android基础新手教程——1.5.2 Git之使用GitHub搭建远程仓库

    Android基础新手教程--1.5.2 Git之使用GitHub搭建远程仓库 标签(空格分隔): Android基础新手教程 本节引言: 在上一节中.我们学习了怎样使用Git.构建我们的本地仓库.轻 ...

  5. 为git创建远程仓库

    首先生成ssh公钥: 将公钥添加到git: 测试秘钥是否通过: 然后就可以到web界面看到标注的地方被绿了: 但是我的没有绿,不知道为啥,难道没有女朋友的原因吗? rm -rf .ssh 重来好几遍都 ...

  6. 使用github作为远程仓库的常见git操作

    [git上传本地代码到github新建仓库]一.建立git本地仓库 1.在本地目标文件夹(Code)中执行命令: git init //初始化本地仓库二.将上传到github的项目文件添加到本地仓库中 ...

  7. Git 创建远程仓库并克隆到本地,创建本地仓库并推送到远程仓库

    配置用户信息 配置的是你个人的用户名称和电子邮件地址.这两条配置很重要,每次 Git 提交时都会引用这两条信息,说明是谁提交了更新,会随更新内容一起被永久纳入历史记录 git config --glo ...

  8. git设置github的远程仓库的相关操作

        git能够把github作为远程仓库,本地可以进行推送有关变更,从而多人可以进行协作开发工作.    1  ssh-keygen -t rsa -C "your-email@163. ...

  9. Git的本地仓库与GitHub的远程仓库

    gitHub是一个面向开源及私有软件项目的托管平台,因为只支持git 作为唯一的版本库格式进行托管,故名gitHub.GitHub 是目前为止最大的开源 Git 托管服务,并且还是少数同时提供公共代码 ...

随机推荐

  1. Python统计列表中的重复项出现的次数的方法

    本文实例展示了Python统计列表中的重复项出现的次数的方法,是一个很实用的功能,适合Python初学者学习借鉴.具体方法如下:对一个列表,比如[1,2,2,2,2,3,3,3,4,4,4,4],现在 ...

  2. 基于FPGA的肤色识别算法实现

    大家好,给大家介绍一下,这是基于FPGA的肤色识别算法实现. 我们今天这篇文章有两个内容一是实现基于FPGA的彩色图片转灰度实现,然后在这个基础上实现基于FPGA的肤色检测算法实现. 将彩色图像转化为 ...

  3. HDU2874 LCA Tarjan

    不知道为什么_add2不能只用单方向呢...........调试了好多次,待我解决这个狗血问题 #include <iostream> #include <vector> #i ...

  4. 江西省移动物联网发展战略新闻发布会举行-2017年10月江西IDC排行榜与发展报告

    编者按:当人们在做技术创新时,我们在做“外包产业“:当人们在做制造产业,我们在做”服务产业“:江人们在做AI智能时,我们在做”物联网“崛起,即使有一个落差,但红色热土从不缺少成长激情. 本期摘自上月初 ...

  5. curl命令用于模拟http浏览器发起动作

    1.模拟http浏览器发起访问百度首页的动作 curl  http://www.baidu.com 2.也可以模拟http浏览器发起POST动作,这个在测试后端程序时非常常见.

  6. 运用 finereport 和 oracle 结合开发报表思路大总结

    近排自己学习了一款软件finereport开发报表模块,自己总结了如何了解需求,分析需求,再进行实践应用开发,最后进行测试数据的准确性,部署报表到项目对应的模块中显示. 一.需求(根据需求文档分析) ...

  7. 【框架学习与探究之定时器--Hangfire】

    声明 本文欢迎转载,请注明文章原始出处:http://www.cnblogs.com/DjlNet/p/7603632.html 前言 在上篇文章当中我们知道关于Quartz.NET的一些情况,其实博 ...

  8. Gridview 动态添加行

    /// <summary> /// 首次加载绑定 /// </summary> private void DataLoad()         { list.Add(new P ...

  9. Java基础-Eclipse环境搭建(02)

    Eclipse工具 IDE(Integrated Development Environment)集成开发环境集成了编写功能,分析功能,编译功能一体化的开发软件. 调试功能等,其中编译在保存时运行(即 ...

  10. PHP设计模式一:工厂方法设计模式

    一.什么是工厂方法模式 作为一种创建型设计模式,工厂方法模式就是要创建“某种东西”.对于工厂方法,要创建的“东西”是一个产品,这个产品与创建它的类之间不存在绑定. 实际上,为了保持这种松耦合,客户会通 ...