创建远程仓库

当你已经在本地创建了一个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. JavaScript中错误正确处理方式,你用对了吗?

    JavaScript的事件驱动范式增添了丰富的语言,也是让使用JavaScript编程变得更加多样化.如果将浏览器设想为JavaScript的事件驱动工具,那么当错误发生时,某个事件就会被抛出.理论上 ...

  2. javascript(js)创建对象的模式与继承的几种方式

    1.js创建对象的几种方式 工厂模式 为什么会产生工厂模式,原因是使用同一个接口创建很多对象,会产生大量的重复代码,为了解决这个问题,产生了工厂模式. function createPerson(na ...

  3. Hibernate Mapping Exception:-9

    if("true".equals(map.get("isAudited"))){ isAudited="=";//已审核 }else{ is ...

  4. var let const 的区别

    Var let const 的区别 1.Var 定义的变量存在变量提升,而了let和const不存在变量提升.即在定义的变量代码上使用该变量,var的会输出undefined,而let的会报错. 2. ...

  5. 扩展js,实现c#中的string.format方便拼接字符串

    //"{0}-{1}-{2}".format("xx","yy","zz") //显示xx-yy-zz String.p ...

  6. gitlab仓库迁移

    遇到一个情况,需要将两个gitlab仓库合并.好在都是使用的ldap账户登陆,用户账户不需要迁移. 实际的使用情况下,需要迁移的主要部分为分组及分组下项目.gitlab的api还是很给力的,能够获取所 ...

  7. 运维&网络知识(一)

    1. DNS 域名系统(Domain Name System),因特网上作为域名和IP地址映射的一个分布式数据库.

  8. 理解HTTPS

    总结HTTPS HTTPS要使客户端与服务器端的通信过程得到安全保证,必须使用的对称加密算法,但是协商对称加密算法的过程,需要使用非对称加密算法来保证安全,然而直接使用非对称加密的过程本身也不安全, ...

  9. btsync 分享资源

    Btsync是一款跨平台软件,可以在不同的设备之间共享文件. Btsync类似于BT下载,用户对用户(多用户)之间的传送. 文档的分享者可以将资源放到文件夹下,生成共享Key,分享给接受者,接受者只需 ...

  10. linux系统下Python虚拟环境的安装和使用

    前言:进行python项目开发的时候,由于不同的项目需要使用不同的资源包和相关的配置,因此创建多个python虚拟环境,在虚拟环境下开发就显得很有必要. 安装虚拟环境 步骤: 打开Linux终端(快捷 ...