Git常用命令

使用Git的大忌:

  • 没什么有价值的修改就提交,也就是没有意义的提交
  • 提交到别人的分支
  • 提交的代码又引入了新的更严重更明显的bug,也就是不如不提交。比第一条更严重,让别人也无法合并

如果git下载不成功,试试下面的代理

1
git clone https://mirror.ghproxy.com/https://github.com/stilleshan/dockerfiles

wget 和 curl 同理
1
2
wget https://mirror.ghproxy.com/https://github.com/stilleshan/dockerfiles/archive/master.zip
curl -O https://mirror.ghproxy.com/https://github.com/stilleshan/dockerfiles/archive/master.zip

配置gitlab密钥

在本地建立仓库到push的过程:

1
2
3
4
5
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/rjosodtssp/Design-Modes.git
git push -u origin master

提交会附带消息和一个哈希值,哈希值是一串包含数字和字母的随机序列。

生成公钥

单独push某文件

1
2
3
git commit test.cpp
//在vim中输入更新信息
git push origin master

删除Repository

在GitHub网站上操作,打开要删除的Repository,点 Settings

Commit message 用 emoji

初次提交示例:

1
git commit -m ":tada: Initialize Repo"

🆕 (全新) :new: 引入新功能
(闪电) :zap: 提升性能
🔖 (书签) :bookmark: 发行/版本标签
🐛 (bug) :bug: 修复 bug
(急救车) :ambulance: 重要补丁

(扳手) :wrench: 修改配置文件
(加号) :heavy_plus_sign: 增加一个依赖
➖ (减号) :heavy_minus_sign: 减少一个依赖
(备忘录) :memo: 撰写文档
(锤子) :hammer: 重大重构
🔥 (火焰) :fire: 移除代码或文件

撤销对文件的修改

test.cpp 修改后,打算返回上次提交的状态,使用命令:

1
git checkout -- qclipper.cpp

如果要撤销 所有文件 的修改,命令为:
1
git checkout -f

git diff ReadMe.mdown 表示在commit之前查看文件修改哪些地方, 在commit之后使用此命令无效。

免密码push的方法

  1. 使用文件创建用户名和密码

文件创建在用户主目录下:

1
2
3
touch .git-credentials
vim .git-credentials
https://{username}:{password}@github.com

记得在真正输入的时候是没有大括号的。

  1. 添加 git config 内容

git config --global credential.helper store

执行此命令后,用户主目录下的.gitconfig 文件会多了一项:[credential]

helper = store

重新 git push 就不需要用户名密码了。

在Github找到以前提交的版本

点击commits
点击红圈的标志,进入旧版本的repository,然后可以下载

Alt text

Git 撤销本地修改

重命名文件

1
2
git mv file.cpp new_file.cpp
git push origin master

从远程仓库拉取

1
2
git init
git clone

修改commit message

  上一次的message如果需要修改,使用:git commit --amend 如果上一次的commit已经push了,那么需要强制提交 git push -f origin master

修改别人的代码

先到别人的项目上fork到自己的GitHub下,clone下来以后进行修改,push到自己的项目。

在自己的项目页面pull request,把我的修改发到对方的项目里,GitHub同时给对方发了邮件,由对方决定是否接受修改。

注意:不要在对方的项目页面clone

查看当前用户名和邮箱

1
2
git config user.name
git config user.email

修改用户名和地址,不加global只能生效一次

1
2
git config --global user.name "your name"
git config --global user.email "your email"

打标签

Git 中的tag指向一次commit的id,通常用来给开发分支做一个标记,如标记一个版本号。

1
git tag -a v1.01 -m "Relase version 1.01"

注解:git tag 是打标签的命令,-a 是添加标签,其后要跟新标签号,-m 及后面的字符串是对该标签的注释。

gitignore

gitignore在本地仓库的根目录,执行文件屏蔽 屏蔽规则

问题累计

对git本地仓库,使用rm而不是git rm删除一个文件A后,直接commit 和 push,远程仓库仍然有A。此时已经不能再执行git rm了,所以将删除动作添加到暂存区 git add .,然后再commit 和 push
操作过程


git add 报错: fatal: in unpopulated submodule

先执行 git rm --cached . -rf,再 git add


github push 代码出现fatal: Authentication failed for

2021.8月份开始远程登录不在支持使用账户密码的方式,解决方法: 把git remote add origin https://github.com/xxx/xxx.git 换成 git remote set-url origin https://<your_token>@github.com/<USERNAME>/<REPO>.git

github获取token