首页 > 代码库 > git 和github简介

git 和github简介

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

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

技术分享

 

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

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

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

技术分享

 

$ git remote add origin git@github.com:LaoNiNi/Test.git
fatal: remote origin already exists.

laoni@DESKTOP-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.

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (master)
$

 

 技术分享

 

 

 

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

 

技术分享

 

技术分享

 

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

技术分享
laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (master)
$ git branch -r
  origin/master

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (master)
$ git branch
* master

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (master)
$ git branch dev

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (master)
$ git branch
  dev
* master

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (master)
$ gir branch -r
bash: gir: command not found

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (master)
$ git branch -r
  origin/master

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (master)
$ git branch dev
fatal: A branch named dev already exists.

laoni@DESKTOP-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.

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (master)
$ git branch -r
  origin/dev
  origin/master

laoni@DESKTOP-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可以删除当前关联项

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (master)
$ git remote -v
origin  git@github.com:LaoNiNi/Test.git (fetch)
origin  git@github.com:LaoNiNi/Test.git (push)

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (master)
$ git remote remove origin

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (master)
$ git remote -v

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

技术分享
$ git add *

laoni@DESKTOP-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


laoni@DESKTOP-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

laoni@DESKTOP-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 git@github.com:LaoNiNi/Test.git

laoni@DESKTOP-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 git@github.com: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.

laoni@DESKTOP-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

laoni@DESKTOP-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 git@github.com: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.

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 (master)
$ lws
bash: lws: command not found

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 (master)
$ git status
On branch master
nothing to commit, working tree clean

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 (master)
$
git pull push

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

技术分享
$ git add README2.md

laoni@DESKTOP-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


laoni@DESKTOP-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(+)

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 (master)
$ git status
On branch master
nothing to commit, working tree clean

laoni@DESKTOP-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 git@github.com: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.

laoni@DESKTOP-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

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 (master)
$ cat README2.md
# Test


This is a new file.

laoni@DESKTOP-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 git@github.com: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.

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 (master)
$ git branch -a
* master
  remotes/origin/master

laoni@DESKTOP-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

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 ((43b2fe4...))
$ cat README2.md
# Test

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

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 ((43b2fe4...))
$ git checkout master
Previous HEAD position was 43b2fe4... second commit
Switched to branch master

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test2 (master)
$ cat README2.md
# Test


This is a new file.

laoni@DESKTOP-TPPLHIB MINGW64 /c/laon
View Code

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

技术分享
laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test ((fc4efc3...))
$ cat README.md
哇哈哈

def adb():
    aa = 1
    bb = 2

laoni@DESKTOP-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(-)

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test ((83aa53b...))
$ cat README.md
哇哈哈6666666666666666666666666666666666

def adb():
    aa = 1
    bb = 2

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

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

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test ((83aa53b...))
$ cat README.md
哇哈哈6666666666666666666666666666666666

def adb():
    aa = 1
    bb = 2

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test ((83aa53b...))
$ git checkout dev
Previous HEAD position was 83aa53b... third remove
Switched to branch dev

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (dev)
$ git branch -a
* dev
  master
  remotes/origin/master

laoni@DESKTOP-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(-)

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (dev)
$ cat README.md
哇哈哈6666666666666666666666666666666666

def adb():
    aa = 1
    bb = 2

laoni@DESKTOP-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

laoni@DESKTOP-TPPLHIB MINGW64 /c/laoni/PycharmProjects/new_test (dev)
$
View Code

 

git 和github简介