创建远程仓库

当你已经在本地创建了一个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. STM8学习

    今天正式学习STM8,用的是风驰STM8S208R开发板. 在编译例程遇到了如下这样的问题. " #error clnk debug\stm8s_demo.lkf:47 can't open ...

  2. 在linux上安装rz、sz包

    在SecureCRT这样的ssh登录软件里, 通过在Linux界面里输入rz/sz命令来上传/下载文件. 对于RHEL5, rz/sz默认没有安装所以需要手工安装.sz: 将选定的文件发送(send) ...

  3. dets

    模块说明 提供基于文件的项式存储,项式以元组表示,其中某个位置为键,默认第1位置 Dets为Mniesia所用,后者增加了事务.查询.和分布式支持. Dets文件不能超过2GB. Dets只有set ...

  4. 【转】FTP主动模式和被动模式的比较

    总是记不住FTP主动和被动模式的区别.放在这里,以备日后查阅.   FTP是仅基于TCP的服务,不支持UDP.与众不同的是FTP使用2个端口,一个数据端口和一个命令端口(也可叫做控制端口).通常来说这 ...

  5. Java面向对象 IO (一)

     Java面向对象  IO  (一) 知识概要:                (1)IO概述 (2)IO流的常用基类 (3)IO程序的书写 (4)字符流  写入  读取  文本文件的两种读取方式 ...

  6. windows2008(64位)下iis7.5中的url伪静态化重写(urlrewrite)

    以前在windows2003里,使用的是iis6.0,那时常使用的URL重写组件是iisrewrite,当服务器升级到windows2008R2时,IIS成了64位的7.5,结果iisreite组件是 ...

  7. 吐槽CSDN--想钱想疯了--阅读全文需要关闭广告屏蔽

    吐槽CSDN 想钱想疯了–阅读全文需要关闭广告屏蔽 近来csdn开始主推博客皮肤升级,说白了就是有一套新的盈利模式,具体怎么操作呢: 1. 采用信息流方式,博客内容变成类似朋友圈.微博.知乎那样的信息 ...

  8. 【转载】jQuery全屏滚动插件fullPage.js

    文章转载自dowebok http://www.dowebok.com/ 原文链接:http://www.dowebok.com/77.html 简介 如今我们经常能见到全屏网站,尤其是国外网站.这些 ...

  9. 安装Sublime Text 3插件的方法:

    安装Sublime Text 3插件的方法: 朋友们,小站活着不容易,全靠广告费养着了,如果本文对你有帮助.麻烦动下手点下页面的广告吧,谢谢! 直接安装 安装Sublime text 2插件很方便,可 ...

  10. 什么是GUID?

    定义及格式 1.全局唯一标识符(GUID,Globally Unique Identifier)是一种由算法生成的二进制长度为128位的数字标识符.GUID主要用于在拥有多个节点.多台计算机的网络或系 ...