记录比较常用的 Git 命令、和新接触的命令。并且记录一些常见问题的解决方案。RUNOOB Git
# Git 常用命令
# 初始化 git 仓库
# 管理远程仓库
| |
| git remote add origin https://github.com/xxx/xxx.git |
| |
| |
| git remote -v |
| |
| |
| git remote rm origin |
# 提交代码
| git push [仓库别名] |
| git push --force |
# 拉取代码(下载)
# 克隆仓库
| git clone https://github.com/xxx/xxx.git |
# 克隆指定分支
| git clone -b <分支名> <仓库地址> |
# git 分支
| |
| git branch -a |
| |
| |
| git branch |
| |
| |
| git branch -D 分支名 |
# 创建与切换分支
| |
| git checkout -b 分支名 [别名/分支名] |
| |
| |
| git checkout 分支名 |
| |
| |
| git checkout - |
# git stash
git stash 是一个用于保存当前工作目录和暂存区(索引)的临时状态的 Git 命令。它允许你在工作目录中有未提交的修改时,将这些修改保存到一个临时的存储区域,然后恢复到工作目录的干净状态。
| |
| git stash |
| |
| |
| git stash apply |
| |
| |
| git stash save "message" |
| |
| |
| git stash apply stash@{0} |
| |
| |
| git stash list |
| |
| |
| git stash drop |
| |
| |
| git stash pop |
| |
| |
| git stash clear |
# git config
# 查看 git config 列表
# git config 全局配置 email 和 name 的设置
| git config --global user.email "[email protected]" |
| git config --global user.name "xxx" |
# 查看 git config 中全局配置 的 email 和 name
| git config --global user.email |
| git config --global user.name |
# git 身份验证
# 个人电脑 SSH 方案
github 关于生成 SSH 的文档
github SSH 添加到 github
也可以阅读博文⭐️SSH 密钥
使用 SSH 的方式,应该使用仓库的 SSH 地址
暂时不确定现在时候需要设置私钥,如果无法使用,就尝试设置。(我貌似没有设置🤣)
- Windows 配置方式
Windows配置方式 | |
| git config --list |
| |
| git config --global core.sshCommand "ssh -i C:\Users\xxx\.ssh\id_rsa" |
| |
| ssh -T [email protected] |
- Ubuntu 配置方式
Ubuntu配置方式这样就可以啦。很方便的使用 Git。
# 公共电脑 个人令牌方案
git 命令中 github 现在无法使用账号密码方式进行身份验证。github 推荐使用 github 个人令牌功能,生成的 token 当作密码使用即可。生成个人令牌步骤如下:
- 进入 github
- 登录 github 账户(账号、密码)
- 右上头像 -> settings
- 点击左侧菜单 Developer Settings
- 点击左侧菜单 Personal access tokens
- 点击左侧菜单 Tokens (classic)
- 点击 Generate new token 进行创建即可(具体权限根据个人需求选择)
- 自己要保存 token,没记住可以回来更新离开 token
通常,我是在服务器上使用个人令牌进行代码 clone。
# 通过个人令牌提交代码
| git remote set-url origin https://<个人令牌>@github.com/<github名字>/<仓库地址>.git |
# 常见问题解决方案
# 远程代码强制覆盖本地代码
| git fetch |
| git reset --hard 仓库别名/分支 |
| git pull |
# 丢弃未提交的更改
# 丢弃所有未提交的更改
# 丢弃某个文件所有未提交的更改
# 代码回滚
# 回退到某个版本 mixed 默认
不删除工作空间改动代码,撤销 commit,并且撤销 git add.
| git reset --mixed HEAD^ |
| git reset HEAD^ |
# 回退到某个版本 soft
不删除工作空间改动代码,撤销 commit,不撤销 git add.
# 回退到上个版本 hard
删除工作空间改动代码,撤销 commit,撤销 git add.
# 回退到 n 次提交之前
# 退到 / 进到,指定 commit 的哈希码
| git reset --hard COMMIT_ID |
# 修改 commit 内容
# 修改本地最新 commit 内容(未 Push 的 commit 内容)
| git commit --amend -m "修改的commit内容" |
# 修改最新 commit 内容
# 修改前几次 commit 内容
- 修改前几次 commit 的内容
- 修改
pick 为 edit
你会看到 pick:******* , 将你要修改内容前的 pick 修改为 edit 后,保存。这时通过 git log 你可以发现,git 的最后一次提交已经变成你选的那个了。 - 修改内容
- 修改内容后,要拿回之前的 log
过去无法挽回,未来可以改变,有的人成日殚精竭虑,却掀不起什么风浪,有的人却因一念之差,让世界天翻地覆,这就是命运权重。