完整版使用手册:https://docs.github.com/zh/get-started
仓库操作
- 初始化仓库
git init
:在当前目录下创建一个新的 Git 仓库。 - 克隆仓库
git clone <repository-url>
:将远程仓库克隆到本地。例如: git clone https://github.com/username/repo.git
提交操作
- 查看状态
git status
:查看当前工作目录和暂存区的文件状态。 - 添加文件到暂存区
git add <file>
:将指定文件添加到暂存区。例如: git add index.html
git add .
:将所有修改和新增的文件添加到暂存区。 - 提交暂存区的文件到本地仓库
git commit -m "commit message"
:将暂存区的文件提交到本地仓库,并添加提交说明。例如: git commit -m "Add new feature"
- 修改上一次提交
git commit --amend
:可以修改上一次提交的说明或添加遗漏的文件。
分支操作
- 查看分支
git branch
:查看本地分支。 git branch -r
:查看远程分支。 git branch -a
:查看所有分支(本地和远程)。 - 创建分支
git branch <branch-name>
:创建一个新的分支。例如: git branch new-feature
- 切换分支
git checkout <branch-name>
:切换到指定分支。例如: git checkout new-feature
git checkout -b <branch-name>
:创建并切换到新分支。例如: git checkout -b new-feature
- 合并分支
git merge <branch-name>
:将指定分支合并到当前分支。例如: git merge new-feature
- 删除分支
git branch -d <branch-name>
:删除本地分支。例如: git branch -d new-feature
git push origin --delete <branch-name>
:删除远程分支。例如: git push origin --delete new-feature
远程仓库操作
- 查看远程仓库
git remote -v
:查看当前配置的远程仓库。 - 添加远程仓库
git remote add <remote-name> <repository-url>
:添加一个新的远程仓库。例如: git remote add origin https://github.com/username/repo.git
- 从远程仓库拉取代码
git pull <remote-name> <branch-name>
:从远程仓库拉取并合并代码。例如: git pull origin main
- 将本地代码推送到远程仓库
git push <remote-name> <branch-name>
:将本地分支的代码推送到远程仓库。例如: git push origin main
查看历史记录
git log
:查看提交历史记录。 git log --oneline
:以简洁的格式查看提交历史记录。
撤销操作
- 撤销工作区的修改
git checkout -- <file>
:撤销指定文件在工作区的修改。例如: git checkout -- index.html
- 撤销暂存区的文件
git reset HEAD <file>
:将指定文件从暂存区移除。例如: git reset HEAD index.html
原文始发于微信公众号(励行安全):写代码怎么能不会用Git
免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论