Git的使用与常见问题解决

这是自己通过Git教程 - 廖雪峰的官方网站学习使用Git整理的笔记,与教程内容有大量重复,侵删。

一、安装Git

Git官网:https://git-scm.com/downloads

安装完成后,进行设置,在命令行输入:

git config --global user.name "Your Name"

git config --global user.email "email@example.com"

注:git config命令的--global参数,表示这台机器上所有的Git仓库都会使用这个配置,当然也可以对某个仓库指定不同的用户名和Email地址。

二、创建版本库

右击欲作为仓库管理的目录,选择Git Bash Here选项

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

注:当前目录下会多一个.git目录,这个目录是Git来跟踪管理版本库的,手动修改这个目录里的文件会破坏Git仓库。如果没有看到.git目录,那是因为这个目录默认是隐藏的。

使用命令git add把文件添加到仓库,可反复多次使用,添加多个文件。

git add readme.txt

使用命令git commit把文件提交到仓库。

git commit -m "添加文件"

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

三、添加远程库

由于本地Git仓库和GitHub仓库之间的传输是通过SSH加密的,所以要创建SSH Key。

ssh-keygen -t rsa -C "youremail@example.com"

在用户主目录里找到.ssh目录,里面有id_rsaid_rsa.pub两个文件,这两个就是SSH Key的秘钥对,id_rsa是私钥,id_rsa.pub是公钥。

登录GitHub,打开“Account settings","SSH Keys"页面,然后,点“Add SSH Key",填上任意Title,在Key文本框里粘贴id_rsa.pub文件的内容。

点"Add Key", 可以看到已经添加的Key。

在本地的仓库下运行命令:

git remote add origin git@github.com:qdu_zyf/learngit.git

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

关联后,使用命令git push -u origin master第一次推送master分支的所有内容。

此后,每次本地提交后,只要有必要,就可以使用命令git push origin master推送最新修改。

四、其他命令

查看已经add 没有commit 的改动

git diff --cached

查看当前没有add和commit的改动:

git diff HEAD
# or
git status

查看任意两个版本之间的改动:

git diff 版本号码1 版本号码2

比较两个版本号码的src 文件夹的差异

git diff 版本号码1 版本号码2 src

五、Git Large File Storage (LFS) 

GitHub的文件最大为100MB,如果文件超过限制,直接上传会报错,需要通过LFS上传 (Git Large File Storage | Git Large File Storage (LFS) replaces large files such as audio samples, videos, datasets, and graphics with text pointers inside Git, while storing the file contents on a remote server like GitHub.com or GitHub Enterprise.)

  1. Download and install the Git command line extension. Once downloaded and installed, set up Git LFS for your user account by running:

    git lfs install

    You only need to run this once per user account.

  2. In each Git repository where you want to use Git LFS, select the file types you'd like Git LFS to manage (or directly edit your .gitattributes). You can configure additional file extensions at anytime.

    git lfs track "*.psd"

    Now make sure .gitattributes is tracked:

    git add .gitattributes

    Note that defining the file types Git LFS should track will not, by itself, convert any pre-existing files to Git LFS, such as files on other branches or in your prior commit history. To do that, use the git lfs migrate(1) command, which has a range of options designed to suit various potential use cases.

  3. There is no step three. Just commit and push to GitHub as you normally would; for instance, if your current branch is named main:

    git add file.psd
    git commit -m "Add design file"
    git push origin main

查找目录下大于100MB的文件

find . -type f -size +100M
# -type f 表示文件
# -size 表示大小

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值