| 目标 | 命令 |
|---|---|
| 丢弃单文件工作区改动 | git restore <file> |
| 取消单文件暂存 | git restore --staged <file> |
| 恢复单文件到某提交 | git restore --source=<hash> <file> |
| 整个仓库回退到某提交 | git reset --hard <hash> |
Add-Content .gitignore "results_demo/"
git rm -r --cached results_demo
git add .gitignore
git commit -m "chore: stop tracking results_demo"
git push origin main
Add-Content .gitignore "results_demo/":把 results_demo/ 加到忽略规则里git rm -r --cached results_demo:只从 Git 索引里删除,不会删你本地磁盘上的文件git push origin main:把这次删除同步到远程 main,这样远程仓库里的 results_demo 也会消失
确认 env/ 不再显示为未跟踪文件
git rm -r --cached env/
git commit -m "Remove env folder from Git"
git status --ignoredGit SSH Key (push public key to github)
ssh-keygen -t ed25519 -C "wli@msbiox.com"
git remote set-url origin git@github.com:liyinred/miniP-backend.git
git remote -v
ssh -T git@github.com# 在ubuntu中还要进行如下前置操作
sudo vim /etc/mysql/mysql.cnf
sudo systemctl restart mysql
[mysqld]
bind-address = 0.0.0.0
# 克隆仓库
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890
git config --global http.proxy socks5://127.0.0.1:7897
git config --global https.proxy socks5://127.0.0.1:7897
git config --list --show-origin
git clone https://github.com/liyinred/ccyy.git
# 进入仓库目录
cd ccyy
# 初始化本地Git仓库
git init
git checkout -b add_lwh
# 配置Git用户信息
git config --global user.name "你的用户名"
git config --global user.email "你的邮箱地址"
# 拉取最新更改
git pull origin main # 如果主分支是main
# 添加当前目录下所有文件到Git暂存区
git add .
# 提交更改到本地仓库
git commit -m "$(date) Commit"
# 连接到GitHub仓库,这里需要替换成你自己的GitHub仓库URL
git remote add origin https://github.com/你的用户名/你的仓库名.git
# 推送到GitHub,默认推送到master分支,如果是main分支则需修改为main
git push -u origin main
# 将本地的master分支 push 到remote中的 Wenhao 分支中
git push origin master:Wenhao
git push --force origin main # 慎用!!!你遇到的错误是由于远程仓库包含本地仓库没有的更新,导致推送被拒绝。这种情况通常发生在其他人已经向远程仓库推送了他们的更改,而你的本地仓库还没有这些更改。为了避免覆盖别人的工作,Git 阻止了你的推送操作。
解决这个问题的步骤如下:
拉取远程更改: 首先,你需要将远程仓库中的最新更改拉取到你的本地仓库中。使用以下命令:
git pull origin main
#放弃所有本地更改
git reset --hard HEAD
git fetch origin
git merge origin/main
git push origin main
git push origin master:wenhao_edit_models_4-28
git clean -n
git clean -f另外一种方法是使用git pull --rebase命令,这样可以避免在历史记录中生成额外的合并提交:
git pull --rebase origin main
git push origin mainerror: Your local changes to the following files would be overwritten by merge
git stash # 暂存本地更改
git pull origin main
git stash pop # 恢复暂存的更改:在pull操作完成后,恢复暂存的更改。Git保存所有的提交,包括已经删除的分支上的提交。你可以使用以下命令找到丢失的提交:
## Git保存所有的提交,包括已经删除的分支上的提交。
git reflog
# 恢复到指定的提交
git checkout 668c50f
# 创建一个新分支保存恢复的数据
git checkout -b restore_branch
# 合并恢复的数据到主分支
git checkout main
git merge restore_branch
# 删除临时恢复的分支
git branch -d restore_branchgit filter-branch -f --env-filter '
OLD_EMAIL="ubuntu@localhost.localdomain"
CORRECT_NAME="wenhao-pc"
CORRECT_EMAIL="liyinred@foxmail.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags