Git及Github使用方法总结

2018web前端开发框架 前端开发框架2018 前端开发app框架

添加SSH认证

  1. 生成SSH密钥

在CODE上查看代码片派生到我的代码片
01.$ ssh-keygen
02.Generating public/private rsa key pair.
03.Enter file in which to save the key (/Users/schacon/.ssh/id_rsa):
04.Enter passphrase (empty for no passphrase):
05.Enter same passphrase again:
06.Your identification has been saved in /Users/schacon/.ssh/id_rsa.
07.Your public key has been saved in /Users/schacon/.ssh/id_rsa.pub.
08.The key fingerprint is:
09.43:c5:5b:5f:b1:f1:50:43:ad:20:a6:92:6a:1f:9a:3a schacon@agadorlaptop.local

  1. 添加公钥到Github

在CODE上查看代码片派生到我的代码片
01.$ cat ~/.ssh/id_rsa.pub
02.ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUpkDHrfHY17SbrmTIpNLTGK9Tjom/BWDSU
03.GPl+nafzlHDTYW7hdI4yZ5ew18JH4JW9jbhUFrviQzM7xlELEVf4h9lFX5QVkbPppSwg0cda3
04.Pbv7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8V6RjsNAQwdsdMFvSlVK/7XA
05.t3FaoJoAsncM1Q9x5+3V0Ww68/eIFmb1zuUFljQJKprrX88XypNDvjYNby6vw/Pb0rwert/En
06.mZ+AW4OZPnTPI89ZPmVMLuayrD2cE86Z/il8b+gw3r3+1nKatmIkjn2so1d01QraTlMqVSsbx
07.NrRFi9wrf+M7Q== schacon@agadorlaptop.local

  1. SSH认证

在CODE上查看代码片派生到我的代码片
01.$ ssh -T git@github.com
02.Hi gotgithub! You’ve successfully authenticated, but GitHub does not provide shell access.

出现上面的提示则认证成功,以后就可以使用ssh协议(自动认证,不用输入口令)克隆远程仓库了。

配置用户信息

  1. 配置全局信息

在CODE上查看代码片派生到我的代码片
01.$ git config –global user.name “John Doe”
02.$ git config –global user.email johndoe@example.com

  1. 配置本地仓库局部信息

首先进入当前仓库工作区

在CODE上查看代码片派生到我的代码片
01.$ git config user.name “John Doe”
02.$ git config user.email johndoe@example.com

  1. 查看当前配置信息

在CODE上查看代码片派生到我的代码片
01.$ git config –list
02.user.name=Scott Chacon
03.user.email=schacon@gmail.com
04.color.status=auto
05.color.branch=auto
06.color.interactive=auto
07.color.diff=auto
08….

  1. 10.$ git config user.name
    11.Scott Chacon

本地仓库常用命令

  1. 初始化
    本地初始化:

在CODE上查看代码片派生到我的代码片
01.$ git init
02.$ git commit -m ‘initial project version
03.$ git remote add origin git@github.com:gotgithub/helloworld.git
04.$ git push -u origin master

或者从远程仓库克隆:

在CODE上查看代码片派生到我的代码片
01.$ git clone git://github.com/schacon/grit.git
02.$ git clone git://github.com/schacon/grit.git mygrit

  1. 检查当前文件状态

在CODE上查看代码片派生到我的代码片
01.$ git status
02.# On branch master
03.nothing to commit (working directory clean)

  1. 添加文件到缓存区

在CODE上查看代码片派生到我的代码片
01.$ vim README
02.$ git status
03.# On branch master
04.# Untracked files:
05.#
06.(use “git add <file>…” to include in what will be committed)
07.#
08.# README
09.nothing added to commit but untracked files present (use “git add” to track)
10.$
11.$ git add README
12.$ git status
13.# On branch master
14.# Changes to be committed:
15.#
16.(use “git reset HEAD <file>…” to unstage)
17.#
18.# new file: README
19.#

  1. 忽略某些文件

在CODE上查看代码片派生到我的代码片
01.$ cat .gitignore
02..[oa]
03.
~

修改.gitignore文件即可。

  1. 查看更新具体内容查看工作目录中当前文件和缓存区域快照之间的差异:

在CODE上查看代码片派生到我的代码片
01.$ git diff
02.diff –git a/benchmarks.rb b/benchmarks.rb
03.index 3cb747f..da65585 100644
04.— a/benchmarks.rb
05.+++ b/benchmarks.rb
06.@@ -36,6 +36,10 @@ def main
07.@commit.parents[0].parents[0].parents[0]
08.end
09.+
10.run_code(x, ‘commits 1’) do
11.+
12.git.commits.size
13.+
14.end
15.+
16.run_code(x, ‘commits 2’) do
17.log = git.commits(‘master’, 15)
18.log.size

查看缓存区文件和上次提交时的快照之间的差异:

在CODE上查看代码片派生到我的代码片
01.$ git diff –cached
02.diff –git a/README b/README
03.new file mode 100644
04.index 0000000..03902a1
05.— /dev/null
06.+++ b/README2
07.@@ -0,0 +1,5 @@
08.+grit
09.+ by Tom Preston-Werner, Chris Wanstrath
10.+ http://github.com/mojombo/grit
11.+
12.+Grit is a Ruby library for extracting information from a Git repository

  1. 提交更新到本地仓库从缓存区提交:

在CODE上查看代码片派生到我的代码片
01.$ git commit -m “Story 182: Fix benchmarks for speed”
02.[master]: created 463dc4f: “Fix benchmarks for speed”
03.2 files changed, 3 insertion

从工作区提交:

在CODE上查看代码片派生到我的代码片
01.$ git commit -a -m ‘added new benchmarks’
02.[master 83e38c7] added new benchmarks
03.1 files changed, 5 insertions(+), 0 deletions(-)

  1. 移除文件

在CODE上查看代码片派生到我的代码片
01.$ rm grit.gemspec
02.$ git status
03.# On branch master
04.#
05.# Changed but not updated:
06.#
07.(use “git add/rm <file>…” to update what will be committed)
08.#
09.#
10.deleted: grit.gemspec
11.#

  1. 13.$ git rm grit.gemspec
    14.rm ‘grit.gemspec’
    15.$ git status
    16.# On branch master
    17.#
    18.# Changes to be committed:
    19.#
    20.(use “git reset HEAD <file>…” to unstage)
    21.#
    22.#
    23.deleted: grit.gemspec
    24.#

直接从缓存区移除文件:

在CODE上查看代码片派生到我的代码片
01.$ git rm –cached readme.txt

  1. 移动文件

在CODE上查看代码片派生到我的代码片
01.$ git mv src/main.cpp ./main.cpp
02.$ git status
03.# On branch master
04.# Your branch is ahead of ‘origin/master’ by 3 commits.
05.#
06.# Changes to be committed:
07.# (use “git reset HEAD <file>…” to unstage)
08.#
09.# renamed: src/main.cpp -> main.cpp
10.#

  1. 查看提交历史

在CODE上查看代码片派生到我的代码片
01.$ git log
02.commit ca82a6dff817ec66f44342007202690a93763949
03.Author: Scott Chacon <schacon@gee-mail.com>
04.Date:
05.Mon Mar 17 21:52:11 2008 -0700
06.changed the verison number
07.commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
08.Author: Scott Chacon <schacon@gee-mail.com>
09.Date:
10.Sat Mar 15 16:40:33 2008 -0700
11.removed unnecessary test code
12.commit a11bef06a3f659402fe7563abf99ad00de2209e6
13.Author: Scott Chacon <schacon@gee-mail.com>
14.Date:
15.Sat Mar 15 10:31:28 2008 -0700
16.first commit

如果使用Github托管项目的话到Github上去看比较清楚。

  1. 覆盖上一次提交

在CODE上查看代码片派生到我的代码片
01.$ git commit -m ‘initial commit’
02.$ git add forgotten_file
03.$ git commit –amend

新增的文件并入到上次提交的内容一起提交,覆盖上次提交。

  1. 取消缓存区中的文件到工作区

在CODE上查看代码片派生到我的代码片
01.$ git reset HEAD benchmarks.rb
02.benchmarks.rb: locally modified
03.$ git status
04.# On branch master
05.# Changes to be committed:
06.#
07.(use “git reset HEAD <file>…” to unstage)
08.#
09.#
10.modified: README.txt
11.#
12.# Changed but not updated:
13.# (use “git add <file>…” to update what will be committed)
14.# (use “git checkout — <file>…” to discard changes in working directory)
15.#
16.#
17.#
18.modified: benchmarks.rb

  1. 新建以及切换分支

在CODE上查看代码片派生到我的代码片
01.$ git branch testing #新建分支

  1. 03.$ git checkout testing #切换分支
  2. 05.$ git checkout -b testing #新建并切换分支
  3. 合并分支

在CODE上查看代码片派生到我的代码片
01.$ git checkout master
02.$ git merge hotfix
03.Updating f42c576..3a0874c
04.Fast forward
05.README |
06.1 –
07.1 files changed, 0 insertions(+), 1 deletions(-)

  1. 删除分支

在CODE上查看代码片派生到我的代码片
01.$ git branch -d hotfix
02.Deleted branch hotfix (3a0874c).

  1. 04.#强制删除未合并的分支
  2. 06.$ git branch -D mybranch1
    07.Deleted branch mybranch1 (was f46a284).
  3. 分支管理

在CODE上查看代码片派生到我的代码片
01.$ git branch iss53
02.* master
03.testing

  1. 05.#查看哪些分支已被并入当前分支
    06.$ git branch –merged
    07.iss53
    08.* master
  2. 10.#查看尚未合并的分支
    11.$ git branch –no-merged testing

与远程仓库交互

  1. 克隆仓库到本地

在CODE上查看代码片派生到我的代码片
01.$ git clone git://github.com/schacon/ticgit.git
02.Initialized empty Git repository in /private/tmp/ticgit/.git/
03.remote: Counting objects: 595, done.
04.remote: Compressing objects: 100% (269/269), done.
05.remote: Total 595 (delta 255), reused 589 (delta 253)
06.Receiving objects: 100% (595/595), 73.31 KiB | 1 KiB/s, done.
07.Resolving deltas: 100% (255/255), done.

  1. 查看当前远程库

在CODE上查看代码片派生到我的代码片
01.$ git remote
02.origin
03.$ git remote -v
04.origin git://github.com/schacon/ticgit.git
05.$ git remote -v
06.bakkdoor git://github.com/bakkdoor/grit.git
07.cho45 git://github.com/cho45/grit.git
08.defunkt git://github.com/defunkt/grit.git
09.koke git://github.com/koke/grit.git
10.origin git@github.com:mojombo/grit.git

  1. 添加远程仓库

在CODE上查看代码片派生到我的代码片
01.$ git remote
02.origin
03.$ git remote add pb git://github.com/paulboone/ticgit.git
04.$ git remote -v
05.origin git://github.com/schacon/ticgit.git
06.pb git://github.com/paulboone/ticgit.git

  1. 从远程仓库抓取数据

在CODE上查看代码片派生到我的代码片
01.$ git fetch [remote-name]

  1. 推送数据到远程仓库

在CODE上查看代码片派生到我的代码片
01.$ git push [remote-name] [branch-name]

  1. 03.$ git push origin master
  2. 05.$ git push [远程名] [本地分支]:[远程分支]
  3. 07.$ git push [远程名] :[远程分支] #删除远程分支
  4. 查看远程仓库信息

在CODE上查看代码片派生到我的代码片
01.$ git remote show [remote-name]

  1. 03.$ git remote show origin
    04.* remote origin
    05.URL: git://github.com/schacon/ticgit.git
    06.Remote branch merged with ‘git pull’ while on branch master
    07.master
    08.Tracked remote branches
    09.master
    10.ticgit
  2. 远程仓库的删除和重命名

在CODE上查看代码片派生到我的代码片
01.$ git remote rename pb paul
02.$ git remote
03.origin
04.paul

  1. 06.$ git remote rm paul
    07.$ git remote
    08.origin
  2. 在远程仓库中创建分支

将mybranch1分支推送到远程仓库中,在远程仓库中新建分支,并添加追踪

在CODE上查看代码片派生到我的代码片
01.$ git push -u origin mybranch1
02.Counting objects: 4, done.
03.Delta compression using up to 2 threads.
04.Compressing objects: 100% (2/2), done.
05.Writing objects: 100% (3/3), 281 bytes, done.
06.Total 3 (delta 0), reused 0 (delta 0)
07.To git@github.com:gotgithub/helloworld.git

    • [new branch] mybranch1 -> mybranch1
      09.Branch mybranch1 set up to track remote branch mybranch1 from origin.

Tag管理

  1. tag的创建

在CODE上查看代码片派生到我的代码片
01.$ git tag <tagname> [<commit>]

  1. 03.$ git tag -m “Tag on initial commit” mytag1 HEAD^
    04.$ git tag -m “Tag on new commit” mytag2
  2. 查看tag

在CODE上查看代码片派生到我的代码片
01.$ git tag -l -n1
02.mytag1 Tag on initial commit
03.mytag2 Tag on new commit

  1. 将本地tag推送到远程仓库中

在CODE上查看代码片派生到我的代码片
01.$ git push origin refs/tags/*
02.Counting objects: 6, done.
03.Delta compression using up to 2 threads.
04.Compressing objects: 100% (4/4), done.
05.Writing objects: 100% (5/5), 548 bytes, done.
06.Total 5 (delta 0), reused 0 (delta 0)
07.To git@github.com:gotgithub/helloworld.git

    • [new tag] mytag1 -> mytag1
    • [new tag] mytag2 -> mytag2
  1. 删除本地tag

在CODE上查看代码片派生到我的代码片
01.$ git tag -d mytag3
02.Deleted tag ‘mytag3’ (was c71231c)

  1. 删除远程仓库中的tag

在CODE上查看代码片派生到我的代码片
01.$ git push origin :mytag3
02.To git@github.com:gotgithub/helloworld.git

  1. [deleted] mytag3

微信服务号开发前端框架 html开发前端框架 前端开发框架知乎

» 本文来自:前端开发者 » 《Git及Github使用方法总结》
» 本文链接地址:https://www.rokub.com/5941.html
» 您也可以订阅本站:https://www.rokub.com
赞(0)
64K

评论 抢沙发

评论前必须登录!