Git 是一个分布式版本控制系统,用于追踪文件和目录的变化。以下是一些 Git 的常用命令:
- 初始化一个新的仓库:
- git init
- 克隆一个仓库:
- git clone <repository_url>
- 添加文件到暂存区:
- git add <file>
- 若要添加所有文件,可以使用:
- git add .
- 提交更改到本地仓库:
- git commit -m "Commit message"
- 查看当前文件状态:
- git status
- 查看提交历史:
- git log
- 创建一个新的分支:
- bashCopy code
- git branch <branch_name>
- 切换到一个分支:
- git checkout <branch_name>
- 或者使用:
- git switch <branch_name>
- 创建并切换到一个新的分支:
- git checkout -b <new_branch_name>
- 或者使用:
- git switch -c <new_branch_name>
- 合并分支:
- git merge <branch_name>
- 拉取远程仓库的变化:
- git pull
- 推送本地更改到远程仓库:
- git push
- 如果是第一次推送到远程仓库,可能需要使用:
- git push -u origin <branch_name>
- 查看远程仓库列表:
- git remote -v
- 查看分支列表:
- git branch
- 撤销工作区的更改:
- git checkout -- <file>
- 撤销暂存区的更改:
- git reset <file>
- 撤销最后一次提交:
- git reset HEAD^
- 这会将最后一次提交的更改放回工作区。
- 删除文件并提交:
- git rm <file> git commit -m "Remove file"
这只是 Git 命令的一小部分,Git 提供了丰富的功能来支持版本控制和团队协作。