github使用教程

1. 安装Git
官网链接 或者 国内镜像
安装完成后,开始->打开git bush
安装完成后,还需要最后一步设置,在命令行输入:``$ git config --global user.name “Your Name”
$ git config --global user.email "email@example.com"`
Git是分布式版本控制系统,所以,每个机器都必须自报家门:你的名字和Email地址。
2. 创建版本库(repository),我把它理解为大管家,在这个版本库下的文件都归它管。

a.在一个合适的地方创建就可以

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~
$ mkdir learngit

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~
$ cd learngit

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit
$ pwd
/c/Users/candiceyj/learngit

b.通过git init 命令把这个目录变成Git可以管理的仓库

$ git init
Initialized empty Git repository in C:/Users/candiceyj/learngit/.git/

告诉我们是一个空的仓库(empty Git repository)
如果你没有看到文件夹里面.git目录,那是因为这个目录默认是隐藏的,用ls -ah命令就可以看见。

$ ls -ah
./  ../  .git/

c.文件添加到版本库
第一步、预处理:
下载Notepad++代替记事本,功能强大,而且免费!把Notepad++的默认编码设置为UTF-8 without BOM即可:
编写一个readme.txt文件,内容如下:
GitHub is so easy.
一定要放到learngit目录下(子目录也行),因为这是一个Git仓库,放到其他地方Git再厉害也找不到这个文件。
第二步、告诉git我要放进去了

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)
$ git add learn.txt

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)
$ git commit -m "wrote a file"
[master (root-commit) e130098] wrote a file
 1 file changed, 1 insertion(+)
 create mode 100644 learn.txt


git commit命令,-m后面输入的是本次提交的说明,可以输入任意内容,当然最好是有意义的,这样你就能从历史记录里方便地找到改动记录。

3. git好处要上场了(包括版本修改,删除,暂存等)
这里想说一下,大概在2018年年末我和小伙伴一起做项目,当时觉得小项目,没有用git,用的tim,结果三人协作的时候尤其是来回传递代码,次数一增加,麻烦随之而来,最后完全 不知道哪个版本代码是先改好的,哪个版本是后改好的,乱成一团,而且还是非常小的项目,于是我们下定决心,下次一定用git,接下来简单介绍一下:
a.继续修改learn.txt文件,改成如下内容:

Git is free software.

git status 查看状态,他告诉我们修改未提交,但是人总是贪心的,总在想要是能发现改什么就好了,
所以git diff

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)
$ git status
On branch master
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:   learn.txt

no changes added to commit (use "git add" and/or "git commit -a")

git diff

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)
$ git diff learn.txt
diff --git a/learn.txt b/learn.txt
index 01c567a..f5b143e 100644
--- a/learn.txt
+++ b/learn.txt
@@ -1 +1 @@
-Github is so easy.
\ No newline at end of file
+Git is free software.

知道了对learn.txt作了什么修改后,再把它提交到仓库就放心多了,提交修改和提交新文件是一样的两步,第一步是git add learn.txt 第二步git commit -m"add a new sentence" 不放心的话随时“git status”

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)
$ git add learn.txt

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   learn.txt


candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)
$ git commit -m "add a new sentence"
[master 2d57b3c] add a new sentence
 1 file changed, 1 insertion(+), 1 deletion(-)


b.git log命令显示从最近到最远的提交日志

如果嫌输出信息太多,可以试试加上–pretty=oneline参数:

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)
$ git log --pretty=oneline
2d57b3c43e324307022e931264626f0c345a1855 (HEAD -> master) add a new sentence
e130098dde5c54e9cda05694e0cca34834c9c20b wrote a file

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)

在Git中,用HEAD表示当前版本,上一个版本就是HEAD^
上上一个版本就是HEAD ^^
当然往上100个版本写100个^比较容易数不过来,所以写成HEAD~100。

现在,要把当前版本回退到上一个版本,就可以使用git reset命令:

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)
$ git reset --hard HEAD^
HEAD is now at e130098 wrote a file

c.git reflog
但是我们总是变来变去,总想吃后悔药,改完又想改回来
Git提供了一个命令git reflog用来记录你的每一次命令:git reset --hard 版本号就ok

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)
$ git reflog
e130098 (HEAD -> master) HEAD@{0}: reset: moving to HEAD^
2d57b3c HEAD@{1}: commit: add a new sentence
e130098 (HEAD -> master) HEAD@{2}: commit (initial): wrote a file

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)
$ git reset --hard 2d57b3c
HEAD is now at 2d57b3c add a new sentence

d.管理修改
cat 查看
增加一些内容,再add

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)
$ cat learn.txt
Git is free software.
candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)
$ git add learn.txt

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   learn.txt


e.撤销删除

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)
$ cat learn.txt
Git is free software.
Git is good.
You are stupid.

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)
$ git checkout -- learn.txt

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)
$ cat learn.txt
Git is free software.
Git is good.

现在假定我们写了一些胡话,还git add到暂存区了:我们总是如此,自行补充
庆幸的是,在commit之前,我们发现了这个问题。用git status查看一下,修改只是添加到了暂存区,还没有提交:


candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)
$ git reset HEAD learn.txt
Unstaged changes after reset:
M       learn.txt

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)
$ git checkout -- learn.txt

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)
$ cat learn.txt
Git is free software.
Git is good.
candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)

终于可以了。
f.删除文件
一般情况下,通常直接在文件管理器中把没用的文件删了,或者用rm命令删了:

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)
$ rm test.txt

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)
$ git rm test.txt
candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)
$ rm 'test.txt'

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)
$ git commit --m "remove test.txt"
On branch master
nothing to commit, working tree clean


先手动删除文件,然后使用git rm 和git add效果是一样的。
万一又删错了,不用担心,版本库里还有呢,所以可以很轻松地把误删的文件恢复到最新版本:
在这里插入图片描述
在这里插入图片描述
4. 远程仓库
github 提供Git仓库托管服务
a.创建SSH Key。
在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,如果已经有了,可直接跳到下一步。如果没有,打开Shell(Windows下打开Git Bash),创建SSH Key:

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)
$ ssh-keygen -t rsa -C "251344815@qq.com"

你需要把邮件地址换成你自己的邮件地址,然后一路回车,使用默认值即可,由于这个Key也不是用于军事目的,所以也无需设置密码。

如果一切顺利的话,可以在用户主目录里找到.ssh目录,里面有id_rsa和id_rsa.pub两个文件,这两个就是SSH Key的秘钥对,id_rsa是私钥,不能泄露出去,id_rsa.pub是公钥,可以放心地告诉任何人。
b.登陆GitHub,打开“Account settings”,“SSH Keys”页面:
然后,点“Add SSH Key”,填上任意Title,在Key文本框里粘贴id_rsa.pub文件的内容:

在这里插入图片描述

在这里插入图片描述
目前,在GitHub上的learngit仓库还是空的,GitHub告诉我们,可以从这个仓库克隆出新的仓库,也可以把一个已有的本地仓库与之关联,然后,把本地仓库的内容推送到GitHub仓库。
c.远程同步
现在,我们根据GitHub的提示,在本地的learngit仓库下运行命令:

$ git remote add origin git@github.com:candicecyj/learngit.git
请千万注意,把上面的candicecyj替换成你自己的GitHub账户名,否则,你在本地关联的就是我的远程库,关联没有问题,但是你以后推送是推不上去的,因为你的SSH Key公钥不在我的账户列表中。

添加后,远程库的名字就是origin,这是Git默认的叫法,也可以改成别的,但是origin这个名字一看就知道是远程库。

下一步,就可以把本地库的所有内容推送到远程库上:

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)
$ git push --u origin master
error: unknown option `u'
usage: git push [<options>] [<repository> [<refspec>...]]

    -v, --verbose         be more verbose
    -q, --quiet           be more quiet
    --repo <repository>   repository
    --all                 push all refs
    --mirror              mirror all refs
    -d, --delete          delete refs
    --tags                push tags (can't be used with --all or --mirror)
    -n, --dry-run         dry run
    --porcelain           machine-readable output
    -f, --force           force updates
    --force-with-lease[=<refname>:<expect>]
                          require old value of ref to be at this value
    --recurse-submodules[=(check|on-demand|no)]
                          control recursive pushing of submodules
    --thin                use thin pack
    --receive-pack <receive-pack>
                          receive pack program
    --exec <receive-pack>
                          receive pack program
    -u, --set-upstream    set upstream for git pull/status
    --progress            force progress reporting
    --prune               prune locally removed refs
    --no-verify           bypass pre-push hook
    --follow-tags         push missing but relevant tags
    --signed[=(yes|no|if-asked)]
                          GPG sign the push
    --atomic              request atomic transaction on remote side
    -o, --push-option <server-specific>
                          option to transmit
    -4, --ipv4            use IPv4 addresses only
    -6, --ipv6            use IPv6 addresses only


candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)
$ git push origin master
The authenticity of host 'github.com (52.74.223.119)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,52.74.223.119' (RSA) to the list of known hosts.
Enumerating objects: 9, done.
Counting objects: 100% (9/9), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (9/9), 705 bytes | 176.00 KiB/s, done.
Total 9 (delta 0), reused 0 (delta 0)
To github.com:candicecyj/learngit.git
 * [new branch]      master -> master

同步后结果
在这里插入图片描述
SSH警告
当你第一次使用Git的clone或者push命令连接GitHub时,会得到一个警告:

The authenticity of host 'github.com (52.74.223.119)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes

这是因为Git使用SSH连接,而SSH连接在第一次验证GitHub服务器的Key时,需要你确认GitHub的Key的指纹信息是否真的来自GitHub的服务器,输入yes回车即可。

Git会输出一个警告,告诉你已经把GitHub的Key添加到本机的一个信任列表里了:

Warning: Permanently added 'github.com,52.74.223.119' (RSA) to the list of known hosts.

5. 从远程库克隆
先创建远程库,然后,从远程库克隆。
首先,登陆GitHub,创建一个新的仓库,名字叫gitskills
我们勾选Initialize this repository with a README,这样GitHub会自动为我们创建一个README.md文件。创建完毕后,可以看到README.md文件:
在这里插入图片描述

远程库已经准备好了,下一步是用命令git clone克隆一个本地库:

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)
$  git clone git@github.com:candicecyj/gitskills.git
Cloning into 'gitskills'...
Warning: Permanently added the RSA host key for IP address '13.229.188.59' to the list of known hosts.
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.


可以查看reademe

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit (master)
$ cd gitskills

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit/gitskills (master)
$ ls
README.md

如果有多个人协作开发,那么每个人各自从远程克隆一份就可以了。

GitHub给出的地址不止一个,还可以用https://github.com/michaelliao/gitskills.git这样的地址。实际上,Git支持多种协议,默认的git://使用ssh,但也可以使用https等其他协议。

使用https除了速度慢以外,还有个最大的麻烦是每次推送都必须输入口令,但是在某些只开放http端口的公司内部就无法使用ssh协议而只能用https。
6.分支管理
a.我们创建dev分支,然后切换到dev分支:

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit/gitskills (master)
$ git checkout -b dev
Switched to a new branch 'dev'

b.git checkout命令加上-b参数表示创建并切换,相当于以下两条命令:

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit/gitskills (dev)
$ git branch dev
fatal: A branch named 'dev' already exists.

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit/gitskills (dev)
$ git checkout dev
Already on 'dev'

git branch命令会列出所有分支,当前分支前面会标一个*号。
然后,我们就可以在dev分支上正常提交,比如对readme.txt做个修改,加上一行:creating a new branch is quick.

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit/gitskills (dev)
$ git branch
* dev
  master

c.提交
dev分支的工作完成,我们就可以切换回master分支:

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit/gitskills (dev)
$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

切换回master分支后,再查看一个readme.txt文件,刚才添加的内容不见了!因为那个提交是在dev分支上,而master分支此刻的提交点并没有变:
现在,我们把dev分支的工作成果合并到master分支上:

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit/gitskills (master)
$ git merge dev
Updating b63f3e7..88269c9
Fast-forward
 readme.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 readme.txt

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit/gitskills (master)
$ cat readme.txt
creating a new branch is quick.
candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit/gitskills (master)
$ git branch -d dev
Deleted branch dev (was 88269c9).

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit/gitskills (master)
$ git branch
* master

git 鼓励我们大量使用分支
在这里插入图片描述

7. 多人协作
a.查看
从远程仓库克隆时,实际上Git自动把本地的master分支和远程的master分支对应起来了,并且,远程仓库的默认名称是origin。
要查看远程库的信息,用git remote: 或者,用git remote -v 显示更详细的信息:

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit/gitskills (master|MERGING)
$ git remote
origin

candiceyj@DESKTOP-VBJ35VJ MINGW64 ~/learngit/gitskills (master|MERGING)
$ git remote --v
origin  git@github.com:candicecyj/gitskills.git (fetch)
origin  git@github.com:candicecyj/gitskills.git (push)

b.推送分支
推送分支,就是把该分支上的所有本地提交推送到远程库。推送时,要指定本地分支,这样,Git就会把该分支推送到远程库对应的远程分支上:

git push origin master
git push origin dev
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值