17.版本控制系统GIT的使用

节选自:《Python编程无师自通》[美]科里·奥尔索夫(Cory Althoff)人民邮电出版社

写在前面

本文主要记录Git的操作命令,作为备忘以便后续使用。

1.GitHub

什么是GitHub
它是一个网上的代码仓库,将你的代码、文件上传到github上可以实现多人协同开发。
网址
https://github.com/
使用方法
注册登录GitHub后点击主页的“Read the guide”,跟随引导一步步学习GitHub的使用,过程比较简单,这里不多赘述。

2.Git
下载安装Git软件包

Git官网下载:https://git-scm.com/downloads
windows系统的Git国内源(速度快):https://npm.taobao.org/mirrors/git-for-windows/

检测安装

在命令行输入git查看Git是否正确安装(可见第二行内容为安装成功)

$ git	
--------以下为输出--------
>>usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
将中央代码仓库下载到本地

中央代码仓库就是GitHub上创建的代码仓库,本步骤将GitHub上的代码同步到本地。
语句:git clone [仓库链接]
仓库链接在如图所示位置
在这里插入图片描述

$ git clone https://github.com/weikoo/python.git
--------以下为输出--------
Cloning into 'python'...
remote: Enumerating objects: 12, done.
remote: Counting objects: 100% (12/12), done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 12 (delta 2), reused 5 (delta 1), pack-reused 0
Receiving objects: 100% (12/12), done.
Resolving deltas: 100% (2/2), done.

同步到本机上的文件在 C:\Users\用户名 文件夹下。linux用户可通过pwd命令确定文件存放目录。

推送和拉取文件

推送(push):将本地文件(的更新)上传到GItHub代码仓库中。
拉取(pull):从GItHub代码仓库下载文件(的更新)到本地。
首先查看代码推送及拉取的网址:

首先要将当前目录切换到本地代码仓库文件夹中
$ cd python

查看代码推送及拉取的网址
$ git remote -v
--------以下为输出--------
第一行是拉取地址,第二行是推送地址

origin  https://github.com/weikoo/python.git (fetch)
origin  https://github.com/weikoo/python.git (push)

推送:
1.暂存(stage)文件,告诉Git希望将哪个文件推送到代码仓库。

$ git status
该命令会把本地代码仓库与中央代码仓库(GitHub)中存在差异的文件打印出来。
未暂存的文件会显示红色,暂存的文件会显示绿色。
--------以下为输出--------
On branch main
Your branch is up to date with 'origin/main'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        new/  <---这个目录未暂存会以红色字体展示

nothing added to commit but untracked files present (use "git add" to track)

暂存命令:git add[文件/目录名]

$ git add new

暂存文件后再次查看文件状态,已经暂存的文件及文件夹会以绿色字体展示。
$ git status
--------以下为输出--------
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   new/123.txt

可以使用git reset [文件路径]取消暂存

2.提交暂存文件
使用命令:git commit -m [要说明的信息]提交文件。

$ git commit -m "我要提交文件啦"
--------以下为输出--------
[main ff8a8ab] 我要提交文件啦
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 new/123.txt

3.将更新推送到中央代码仓库中
使用命令:git push origin [branch名称]
默认的branch名称是master或main

$ git push origin main
--------以下为输出--------
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 328 bytes | 328.00 KiB/s, done.
Total 4 (delta 1), reused 1 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/weikoo/python.git
   6b215c2..ff8a8ab  main -> main

在命令行输入用户名和密码后,Git就会将本地修改推送至GitHub。
使用浏览器登录GitHub就可以看到本次更新了。

拉取:

命令:git pull origin [branch名称]

$ git pull origin main
--------以下为输出--------
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 4 (delta 1), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), 717 bytes | 11.00 KiB/s, done.
From https://github.com/weikoo/python
 * branch            main       -> FETCH_HEAD
   ac85228..7977048  main       -> origin/main
Updating ac85228..7977048
Fast-forward
 new/123.txt | 1 +
 1 file changed, 1 insertion(+)
回退版本

每次提交一个文件,Git就会保存项目代码。通过Git,我们可以回退到任意一次代码提交,即可以做到回溯。每次提交都有一个提交编号 :Git用来标记提交的唯一一组字符串序列。
命令:git log 查看项目提交历史

--------以下为输出--------
commit 797704864771bab30dc170587ed2ae671c9fa3b0 (HEAD -> main, origin/main, origin/HEAD)
Author: 晴天 <35066175+weikoo@users.noreply.github.com>
Date:   Tue Nov 17 00:07:09 2020 +0800

    Update 123.txt

commit ac8522882cbeb89e1bad1ce9008441d1791173b6
Merge: 51f52ae d9c09fe
Author: weikoo <1025648849@qq.com>
Date:   Tue Nov 17 00:04:28 2020 +0800

    Merge branch 'main' of https://github.com/weikoo/python into main
     the commit.

commit 51f52ae1ecadea735191674b8e4da63171b1b453
Author: weikoo <1025648849@qq.com>
Date:   Tue Nov 17 00:01:23 2020 +0800

    我要提交文件啦

以上一共有三次提交,commit后面的就是提交编号,使用命令:git checkout [提交编号]来切换到对应的提交版本。

git checkout d9c09fe00786e2014ed5e50297e3f3cd0cccff04
--------以下为输出--------
Note: switching to 'd9c09fe00786e2014ed5e50297e3f3cd0cccff04'.

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 switching back to a branch.

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

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at d9c09fe Delete 123.txt

至此Git的基本使用方法介绍完毕。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值