git 和github简介

关于github不清楚的可以百度,

在这里,可以创建一个新的仓库

点击Create repository后会出现下面这些信息,其中第一块是仓库的url链接

第二块是你在本地目录中创建一个READEME.md文件,然后进行初始化,在add和commit提交到分支,然后再关联仓库,最后通过push 推送本地代码到github。

第三块其实就是已创建了本地仓库的前提下,执行第二块后面两行命令,推送代码到github

$ git remote add origin [email protected]:LaoNiNi/Test.git
fatal: remote origin already exists.

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (master)
$ git push -u origin master
Counting objects: 2, done.
Delta compression using up to 6 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 235 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To github.com:LaoNiNi/Test.git
   87faef8..43b2fe4  master -> master
Branch master set up to track remote branch master from origin.

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (master)
$

现在我们可以通过url来把代码clone到本地

通过 git branch -r 可以查看本地和远程相关联的git分支有哪些。通过push时指定提交分支,可以让分支和远程分支关联。

[email protected] MINGW64 /c/laoni/PycharmProjects/new_test (master)
$ git branch -r
  origin/master

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (master)
$ git branch
* master

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (master)
$ git branch dev

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (master)
$ git branch
  dev
* master

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (master)
$ gir branch -r
bash: gir: command not found

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (master)
$ git branch -r
  origin/master

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (master)
$ git branch dev
fatal: A branch named ‘dev‘ already exists.

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (master)
$ git push -u origin dev
Total 0 (delta 0), reused 0 (delta 0)
To github.com:LaoNiNi/Test.git
 * [new branch]      dev -> dev
Branch dev set up to track remote branch dev from origin.

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (master)
$ git branch -r
  origin/dev
  origin/master

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (master)
$

git branch

当别的同事create和push新的分支到github时,我可以通过git branch -a 查看本地分支和远程分支有哪些,我们clone代码时只会把默认分支(master)clone到本地,并不包括别的分支,所以需要通过 git checkout -b bug /origin/bug 命令创建新分支,并且切换到新分支,而且关联远程的bug分支。

通过git remote -v 可以查看当前关联哪个远程git,通过git remote remove origin可以删除当前关联项

[email protected] MINGW64 /c/laoni/PycharmProjects/new_test (master)
$ git remote -v
origin  [email protected]:LaoNiNi/Test.git (fetch)
origin  [email protected]:LaoNiNi/Test.git (push)

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (master)
$ git remote remove origin

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (master)
$ git remote -v

使用git pull origin 分支名称 可以更新远程分支,并且合并我当前分支,如果有冲突,会在本地有冲突的文件里出现提示。

$ git add *

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 (master)
$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   README.md
        new file:   README2.md

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 (master)
$ git commit -m "commit newfile"
[master (root-commit) ae86fce] commit newfile
 2 files changed, 2 insertions(+)
 create mode 100644 README.md
 create mode 100644 README2.md

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 (master)
$ git push origin dev
error: src refspec dev does not match any.
error: failed to push some refs to ‘[email protected]:LaoNiNi/Test.git‘

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 (master)
$ git push origin master
To github.com:LaoNiNi/Test.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to ‘[email protected]:LaoNiNi/Test.git‘
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., ‘git pull ...‘) before pushing again.
hint: See the ‘Note about fast-forwards‘ in ‘git push --help‘ for details.

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 (master)
$ git pull origin master
warning: no common commits
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 5 (delta 0), reused 5 (delta 0), pack-reused 0
Unpacking objects: 100% (5/5), done.
From github.com:LaoNiNi/Test
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
fatal: refusing to merge unrelated histories

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 (master)
$ git push origin master
To github.com:LaoNiNi/Test.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to ‘[email protected]:LaoNiNi/Test.git‘
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: ‘git pull ...‘) before pushing again.
hint: See the ‘Note about fast-forwards‘ in ‘git push --help‘ for details.

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 (master)
$ lws
bash: lws: command not found

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 (master)
$ git status
On branch master
nothing to commit, working tree clean

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 (master)
$

git pull push

但是git pull会自动帮我们合并分支,帮我们隐藏了很多细节,这样不能很好的掌控代码变化细节,

$ git add README2.md

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   README2.md

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 (master)
$ git commit -m "提交 This is a new file"
[master 6efcf83] 提交 This is a new file
 1 file changed, 3 insertions(+)

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 (master)
$ git status
On branch master
nothing to commit, working tree clean

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 (master)
$ git push origin master
To github.com:LaoNiNi/Test.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to ‘[email protected]:LaoNiNi/Test.git‘
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: ‘git pull ...‘) before pushing again.
hint: See the ‘Note about fast-forwards‘ in ‘git push --help‘ for details.

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 (master)
$ git pull origin master
From github.com:LaoNiNi/Test
 * branch            master     -> FETCH_HEAD
fatal: refusing to merge unrelated histories

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 (master)
$ cat README2.md
# Test

This is a new file.

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 (master)
$ git push origin master
To github.com:LaoNiNi/Test.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to ‘[email protected]:LaoNiNi/Test.git‘
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: ‘git pull ...‘) before pushing again.
hint: See the ‘Note about fast-forwards‘ in ‘git push --help‘ for details.

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 (master)
$ git branch -a
* master
  remotes/origin/master

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 (master)
$ git checkout origin/master
Note: checking out ‘origin/master‘.

You are in ‘detached HEAD‘ state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at 43b2fe4... second commit

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 ((43b2fe4...))
$ cat README2.md
# Test

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 ((43b2fe4...))
$ git branch -a
* (HEAD detached at origin/master)
  master
  remotes/origin/master

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 ((43b2fe4...))
$ git checkout master
Previous HEAD position was 43b2fe4... second commit
Switched to branch ‘master‘

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 (master)
$ cat README2.md
# Test

This is a new file.

[email protected]-TPPLHIB MINGW64 /c/laon

所以我们合并分支时,可以用merge  加上  --no-ff参数这样有问题可以撤销:

[email protected] MINGW64 /c/laoni/PycharmProjects/new_test ((fc4efc3...))
$ cat README.md
哇哈哈

def adb():
    aa = 1
    bb = 2

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test ((fc4efc3...))
$ git pull origin master
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (3/3), done.
Unpacking objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
From github.com:LaoNiNi/Test
 * branch            master     -> FETCH_HEAD
   fc4efc3..83aa53b  master     -> origin/master
Updating fc4efc3..83aa53b
Fast-forward
 README.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test ((83aa53b...))
$ cat README.md
哇哈哈6666666666666666666666666666666666

def adb():
    aa = 1
    bb = 2

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test ((83aa53b...))
$ git checkout origin/master
HEAD is now at 83aa53b... third remove

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test ((83aa53b...))
$ git branch -a
* (HEAD detached from d405ab6)
  dev
  master
  remotes/origin/master

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test ((83aa53b...))
$ cat README.md
哇哈哈6666666666666666666666666666666666

def adb():
    aa = 1
    bb = 2

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test ((83aa53b...))
$ git checkout dev
Previous HEAD position was 83aa53b... third remove
Switched to branch ‘dev‘

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (dev)
$ git branch -a
* dev
  master
  remotes/origin/master

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (dev)
$ git merge origin/master --no-ff
Merge made by the ‘recursive‘ strategy.
 README.md  | 6 +++++-
 README2.md | 4 ++++
 2 files changed, 9 insertions(+), 1 deletion(-)

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (dev)
$ cat README.md
哇哈哈6666666666666666666666666666666666

def adb():
    aa = 1
    bb = 2

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (dev)
$ git log --oneline
bbb0d30 Merge remote-tracking branch ‘origin/master‘ into dev
83aa53b third remove
fc4efc3 哇哈哈
d405ab6 second remove
9902110 README.md remove
98aa011 abc
6efcf83 提交 This is a new file
ae86fce commit newfile
43b2fe4 second commit
87faef8 first commit

[email protected]-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (dev)
$

时间: 2024-10-12 14:19:26

git 和github简介的相关文章

Git和Github的介绍、简单操作

目的:   1.git与github简介  2.Git与SVN区别 3.Github 的简单使用 4.下载安装Git-20-64-bit.exe   5.Git常用命令 5.1Git命令使用场景 5.2常用命令 5.3实践操作:使用git提交文件至远程仓库 Git与Github简介 Git简介:Git是一个开源的[分布式][版本控制系统],用于敏捷高效地处理任何或小或大的项目 版本控制器: CVS/SVN/Git SVN: 客户端/服务器 GIT: 客户端/代码托管网站(例如:github) 注

Git&amp;GitHub简介与入手(一)

一.Git版本控制 1.集中式版本控制工具:SVN(版本控制集中在服务器端,会有单点故障风险): 2.分布式版本控制工具:Git: 3.Git简史 Talk is cheap, show me the code.(Linus) 4.https://git-scm.com/ 官网下载安装git (后面均使用默认设置): 二.Git&GitHub简介 1.Git的结构与GitHub的关系 2.Git&GitHub---本地库与远程库的交互方式 (1)团队内的合作 (2)跨团队协作 三.Git命

git与github区别与简介

git和github是两个完全不同的概念. git      是一个版本管理工具,是可以在你电脑不联网的情况下,只在本地使用的一个版本管理工具,其作用就是可以让你更好的管理你的程序,比如你原来提交过的内容,以后虽然修改了,但是通过git这个工具,可以把你原来提交的内容重现出来,这样对于你后来才意识到的一些错误的更改,可以进行还原. 关于github,这是一个网站,就是每个程序员自己写的程序,可以在github上建立一个网上的仓库,你每次提交的时候可以把代码提交到网上,这样你的每次提交,别人也都可

git和github

git: git是一个版本管理工具,是可以在你电脑不联网的情况下,只在本地使用的一个版本管理工具,其作用就是可以让你更好的管理你的程序,比如你原来提交过的内容,以后虽然修改了,但是通过git这个工具,可以把你原来提交的内容重现出来,这样对于你后来才意识到的一些错误的更改,可以进行还原,具体有关git的简介,我觉得有一个教程写的非常的详细,比较适合新手的学习,这是链接:Git教程 github:  这是一个网站,就是每个程序员自己写的程序,可以在github上建立一个网上的仓库,你每次提交的时候可

【git】git与github的英文记录

Pull requests  Issues Gist 请求 问题 要点 ------------------------------------------------------------------------------------------- Learn Git and GitHub without any code! 没有任何代码学习Git和GitHub! ---------------------------------------------------------------

Git 学习笔记&lt;简介与安装&gt; (一)

Git,开源中国以及GitHub所使用的系统, Is A 一个分布式版本控制系统 Be Used to 为团队合作写代码提供方便的管理系统.几乎满足你所有关于合作写代码的幻想. Has 本地端:工作区.版本库 (版本库还含有一个暂存区) 远程仓库:版本库(用来储存版本库的服务器) How To Install Linux: 首先,先输入git,看看是否安装Git: $ gitThe program 'git' is currently not installed. You can install

GitHub 简介与优势

GitHub 简介 Git 是一个优秀的分布版本控制系统.版本控制系统可以保留一个文件集合的历史记录,并能回滚文件集合到另一个状态(历史记录状态).另一个状态可以是不同的文件,也可以是不同的文件内容.在一个分布版本控制系统中,每个人都有一份完整的源代码(包括源代码所有的历史记录信息),而且可以对这个本地的数据进行操作.分布版本控制系统不需要一个集中式的代码仓库. GitHub 是一个面向开源及私有软件项目的托管平台,因为只支持 Git 作为唯一的版本库格式进行托管,故名 GitHub. GitH

[git与github]__git与github简单了解

前言 关于版本控制,版本控制是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统. 我们通过版本控制系统可以解决的问题:可以非常方便的查看我们的源码文件的历代更新版本.或将我们的项目源代码文件回溯到之前的某一版本. 关于版本控制的发展历史可以参看: pro git__起步 git简介 git是一个分布式版本控制软件,最初由最初由林纳斯·托瓦兹创作,于2005年以GPL发布.最初目的是为更好地管理Linux内核开发而设计. git可以把档案的状态作为更新历史记录保存起来.因此可以把

Ubuntu Linux下通过代理(proxy)使用git上github.com

github.com,作为程序员的代码仓库,我们经常会用到.但有时候我们不能直接通过网络链接它,只能通过代理. 这里我有一台代理服务器,起初我以为在终端设置了代理环境就行了,其设置为在你的~/.bashrc里增加以下几行: export http_proxy="http://proxy-server:3128/" export https_proxy="http://proxy-server:3128/" export ftp_proxy="http://