Git 手册

  1. git 全局设置
git config --global user.name "openHacking"
git config --global user.email "openHacking@126.com"
  1. 创建一个新仓库
git clone https://github.com/openHacking/xhack.git
cd xhack
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master
  1. 推送已存在的文件夹
cd xhack
git init
git remote add origin https://github.com/openHacking/xhack.git
git add .
git commit -m "Initial commit"
git push -u origin master
  1. 设置本地账户并推送已存在的文件夹
git init
git config --local user.name "openHacking"
git config --local user.email "openHacking@126.com"
git add .
git commit -m "init"
git branch -M main
git remote add origin https://openHacking@github.com/openHacking/xhack.git
git config --local credential.helper store
git push -u origin main
  1. 推送已存在的仓库
cd xhack
git remote rename origin old-origin
git remote add origin https://github.com/openHacking/xhack.git
git push -u origin --all
git push -u origin --tags
  1. 修改仓库原始 origin 地址
git remote set-url origin https://openHacking@github.com/openHacking/xhack.git
  1. git 删除所有的 commit 提交历史

删除 .git 文件夹可能会导致 git 仓库出现问题。如果你想删除所有提交历史记录,但将代码保持在当前状态,你可以安全地执行以下操作:

# 尝试运行
git checkout --orphan latest_branch

# 添加所有文件
git add -A

# 提交更改
git commit -am "commit message"

# 删除分支
git branch -D master

# 重命名当前分支
git branch -m master

# 后,强制更新仓库
git push -f origin master
  1. 仅推送 tag 标签
# 查看最新标签
git describe

# 生成新标签
git tag -a release-v0.0.0 -m "msg"

# 推送标签
git push origin release-v0.0.0
  1. 推送提交 commit 并带上 tag 标签
git add .
git commit -m "msg"
git pull
git describe
git tag -a release-v0.0.0 -m "msg"
git push --follow-tags
  1. git 撤销 commit
git reset HEAD .
  1. 新建分支

比如新建 dev 分支

git checkout -b dev
  1. 合并分支

如何将开发分支合并到主分支?

合并前记得先提交自己的 dev 分支的代码

# 1. 进入要合并的分支(如果dev分支合并到main,进入main目录)
git checkout main
git pull

# 2. 检查是否所有分支都被拉到最新
git branch -a

# 3. 使用merge合并dev分支
git merge dev

# 4. 查看合并后的状态
git status

# 5. 如果有冲突,通过IDE解决冲突

# 6. 解决冲突后,提交冲突文件到暂存区
git add .

# 7. 提交合并后的结果
git commit -m "fix conflict"

# 8. 提交本地仓库代码到远程仓库
git push