Git 使用指南
Git 使用指南
Git 是一个分布式版本控制系统,用于跟踪计算机文件的变化,并协调多人共同合作的项目。以下是 Git 的基本使用方法和常见命令。
安装 Git
Windows:
- 访问 Git 官方网站 下载适合 Windows 的安装程序。
- 按照安装向导的指示进行安装。
macOS:
使用 Homebrew 安装:
1
brew install git
或者通过 macOS 的软件包管理器安装。
Linux:
Ubuntu/Debian:
1
sudo apt-get install git
CentOS/RHEL:
1
sudo yum install git
配置 Git 用户信息
1 | git config --global user.name "Your Name" |
创建本地仓库
初始化仓库:
1
git init
克隆远程仓库:
1
git clone [repository-url]
基本工作流程
查看状态:
1
git status
添加文件到暂存区:
1
git add [file]
添加所有改动:
1
git add .
提交暂存区的改动:
1
git commit -m "Commit message"
分支管理
查看分支:
1
git branch
创建新分支:
1
git branch [branch-name]
切换分支:
1
git checkout [branch-name]
合并分支:
1
git merge [branch-name]
删除分支:
1
git branch -d [branch-name]
远程仓库操作
添加远程仓库:
1
git remote add origin [repository-url]
查看远程仓库信息:
1
git remote -v
拉取远程仓库的最新改动:
1
git pull origin [branch-name]
推送本地改动到远程仓库:
1
git push origin [branch-name]
推送新分支到远程仓库:
1
git push --set-upstream origin [branch-name]
删除远程分支:
1
git push origin --delete [branch-name]
解决冲突
查看冲突文件:
1
git status
手动解决冲突:
- 打开冲突文件,解决冲突部分。
- 保存文件并重新添加到暂存区。
1
git add [conflicted-file]
提交解决后的文件:
1
git commit -m "Resolved conflict"
日常操作
查看提交历史:
1
git log
撤销最近一次提交:
1
git reset --hard HEAD~1
撤销暂存区的文件:
1
git reset [file]
恢复已删除的文件:
1
git checkout -- [file]
查看差异:
1
git diff [file]
标签管理:
1
2git tag [tag-name]
git push origin [tag-name]重命名分支:
1
git branch -m [new-branch-name]
合并分支时忽略冲突:
1
git merge --no-commit [branch-name]
合并分支时保留历史记录:
1
git merge --no-commit --no-ff [branch-name]
删除本地标签:
1
git tag -d [tag-name]
删除远程标签:
1
git push origin :refs/tags/[tag-name]
常见问题及解决方法
无法推送更改
检查远程仓库 URL:
1
git remote -v
确认远程仓库 URL 是否正确:
1
git remote set-url origin [correct-repository-url]
解决认证问题:
使用个人访问令牌(PAT):
1
git push https://<PAT>@github.com/your-repo.git
无法删除文件
确认文件是否已添加到暂存区:
1
git status
删除文件并从暂存区移除:
1
git rm [file]
提交删除操作:
1
git commit -m "Delete file"
无法解决冲突
查看冲突文件:
1
git status
手动解决冲突:
- 打开冲突文件,解决冲突部分。
- 保存文件并重新添加到暂存区。
1
git add [conflicted-file]
提交解决后的文件:
1
git commit -m "Resolved conflict"
结论
通过以上步骤,你可以有效地使用 Git 进行版本控制和协作。如果你在使用过程中遇到任何问题,可以查阅 Git 的官方文档或寻求社区支持。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 Oh My Rain!
