github创建远程仓库
创建远程仓库
当你已经在本地创建了一个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分支是否推到远程,取决于你是否和你的小伙伴合作在上面开发
多人协作的工作模式通常是这样:
首先,可以试图用
git push origin branch-name
推送自己的修改;如果推送失败,则因为远程分支比你的本地更新,需要先用
git pull
试图合并;如果合并有冲突,则解决冲突,并在本地提交;
没有冲突或者解决掉冲突后,再用
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创建远程仓库的更多相关文章
- git在本地创建远程仓库
类似的博文,在前面的帖子里面也提到过,当时讲述的是一个入门级别的.其URL是ssh://username@repo-host-address/repo-path这种格式. 今天再说说如何创建类似Git ...
- windows下使用git和github建立远程仓库
转自(http://www.bubuko.com/infodetail-430228.html) 从昨天开始就在看git的使用,因为在Windows下很多命令行操作都比较坑爹,但是今天再走了无数弯路之 ...
- git的使用(包括创建远程仓库到上传代码到git的详细步骤以及git的一些常用命令)
A创建远程仓库到上传代码到git 1)登陆或这注册git账号 https://github.com 2)创建远程仓库 3)打开终端输入命令 cd到你的本地项目根目录下,执行如下git命令 git in ...
- Android基础新手教程——1.5.2 Git之使用GitHub搭建远程仓库
Android基础新手教程--1.5.2 Git之使用GitHub搭建远程仓库 标签(空格分隔): Android基础新手教程 本节引言: 在上一节中.我们学习了怎样使用Git.构建我们的本地仓库.轻 ...
- 为git创建远程仓库
首先生成ssh公钥: 将公钥添加到git: 测试秘钥是否通过: 然后就可以到web界面看到标注的地方被绿了: 但是我的没有绿,不知道为啥,难道没有女朋友的原因吗? rm -rf .ssh 重来好几遍都 ...
- 使用github作为远程仓库的常见git操作
[git上传本地代码到github新建仓库]一.建立git本地仓库 1.在本地目标文件夹(Code)中执行命令: git init //初始化本地仓库二.将上传到github的项目文件添加到本地仓库中 ...
- Git 创建远程仓库并克隆到本地,创建本地仓库并推送到远程仓库
配置用户信息 配置的是你个人的用户名称和电子邮件地址.这两条配置很重要,每次 Git 提交时都会引用这两条信息,说明是谁提交了更新,会随更新内容一起被永久纳入历史记录 git config --glo ...
- git设置github的远程仓库的相关操作
git能够把github作为远程仓库,本地可以进行推送有关变更,从而多人可以进行协作开发工作. 1 ssh-keygen -t rsa -C "your-email@163. ...
- Git的本地仓库与GitHub的远程仓库
gitHub是一个面向开源及私有软件项目的托管平台,因为只支持git 作为唯一的版本库格式进行托管,故名gitHub.GitHub 是目前为止最大的开源 Git 托管服务,并且还是少数同时提供公共代码 ...
随机推荐
- windows无法启动MySQL服务 错误1067
启动wampmysqld 出现 1067 错误 解决方法:删除在MySQL安装目录下的Data目录中的ib_logfile0和ib_logfile1这两个文件. 重新启动MySQL服务
- CPU工作方式、多核心、超线程技术详解[转贴]
CPU是一台电脑的灵魂,决定电脑整体性能.现在的主流CPU都是多核的,有的运用了多线程技术(Hyper-threading,简称HT).多核可能还容易理解些,相信不少玩家都能说出个所以然.但超线程是个 ...
- 寻找bug并消灭系列——记录在Android开发所遇到的bug(二)
bug 1: bug描述: 无法成功地将edittext中的内容传入数据库中 bug动图: 经过: 最近写了个项目,项目要使用到SQL数据库,由于没有相关知识,便是找到了各种资料开始了自学之旅,在de ...
- sublime text注册码(秘钥)
—– BEGIN LICENSE —– TwitterInc 200 User License EA7E-890007 1D77F72E 390CDD93 4DCBA022 FAF60790 61AA ...
- IDL 矩阵运算
矩阵相乘,A#B表示A的列乘以B的行,要求A的行数必须跟B的列数一致 IDL> A=[[0,1,2],[3,4,5]] IDL> B=[[0,1],[2,3],[4,5]] IDL> ...
- 分布式锁的实现(redis)
1.单机锁 考虑在并发场景并且存在竞态的状况下,我们就要实现同步机制了,最简单的同步机制就是加锁. 加锁可以帮我们锁住资源,如内存中的变量,或者锁住临界区(线程中的一段代码),使得同一个时刻只有一个线 ...
- opencv-python:win7下,搭建python2.7.5环境,配置opencv3.1.0准备开工-OpenCV步步精深
我的个人博客:点这里 搭建python2.7.5环境 下载python2.7.5 64位:https://www.python.org/ftp/python/2.7.5/python-2.7.5.am ...
- CVPixelBuffer的创建 数据填充 以及数据读取
CVPixelBuffer的创建数据填充以及数据读取 CVPixelBuffer 在音视频编解码以及图像处理过程中应用广泛,有时需要读取内部数据,很少的时候需要自行创建并填充数据,下面简单叙述. 创建 ...
- gevent的同步与异步
from gevent import spawn,joinall,monkey;monkey.patch_all() import time def task(pid): time.sleep(0.5 ...
- 关于Java和JavaScript对字符串截取处理的总结
在JavaWeb开发中,经常需要对字符串进行处理,包括Java语言和JS语言,总是容易弄混淆,这里简单对比一下两种语言对于字符串截取方法. 一.先看Java public class StringDe ...