github创建远程仓库

时间:2021-12-08 10:35:18

创建远程仓库

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

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

github创建远程仓库

下载代码
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.

github创建远程仓库

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

[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 恢复并删除临时保存的备份

常用命令