Git 常用操作命令说明

Git 常用操作命令

1. 初始化和克隆仓库

1.1 初始化仓库

bash 复制代码
git init

在当前目录初始化一个新的 Git 仓库。

1.2 克隆仓库

bash 复制代码
git clone <repository-url>

从远程仓库克隆项目到本地。

示例

bash 复制代码
git clone https://github.com/user/repo.git

2. 查看状态和日志

2.1 查看状态

bash 复制代码
git status

查看当前工作目录和暂存区的状态。

2.2 查看提交历史

bash 复制代码
git log

查看项目的提交历史。

3. 文件操作

3.1 添加文件到暂存区

bash 复制代码
git add <file>

将指定文件添加到暂存区。

示例

bash 复制代码
git add README.md

添加所有更改的文件:

bash 复制代码
git add .

3.2 提交更改

bash 复制代码
git commit -m "commit message"

将暂存区的更改提交到本地仓库。

示例

bash 复制代码
git commit -m "Added new feature"

4. 分支操作

4.1 创建新分支

bash 复制代码
git branch <branch-name>

创建一个新分支。

示例

bash 复制代码
git branch feature-branch

4.2 切换分支

bash 复制代码
git checkout <branch-name>

切换到指定的分支。

示例

bash 复制代码
git checkout feature-branch

4.3 创建并切换分支

bash 复制代码
git checkout -b <branch-name>

创建一个新分支并切换到该分支。

示例

bash 复制代码
git checkout -b new-feature

4.4 查看所有分支

bash 复制代码
git branch

列出所有本地分支。

5. 合并与冲突

5.1 合并分支

bash 复制代码
git merge <branch-name>

将指定分支的更改合并到当前分支。

示例

bash 复制代码
git merge feature-branch

5.2 解决合并冲突

在出现合并冲突时,手动解决冲突后,使用:

bash 复制代码
git add <file>
git commit -m "resolved merge conflict"

6. 远程操作

6.1 查看远程仓库

bash 复制代码
git remote -v

查看已添加的远程仓库。

6.2 添加远程仓库

bash 复制代码
git remote add <name> <repository-url>

添加一个新的远程仓库。

示例

bash 复制代码
git remote add origin https://github.com/user/repo.git

6.3 推送更改到远程仓库

bash 复制代码
git push <remote> <branch>

将本地分支的更改推送到远程仓库。

示例

bash 复制代码
git push origin main

6.4 拉取远程更改

bash 复制代码
git pull <remote> <branch>

从远程仓库拉取并合并更改。

示例

bash 复制代码
git pull origin main

7. 其他常用命令

7.1 查看文件的修改历史

bash 复制代码
git blame <file>

查看指定文件的每一行的最后修改者和修改时间。

7.2 撤销更改

  • 撤销工作目录中的更改:

    bash 复制代码
    git checkout -- <file>
  • 撤销暂存区中的更改:

    bash 复制代码
    git reset <file>

7.3 删除分支

bash 复制代码
git branch -d <branch-name>

删除指定的本地分支。

示例

bash 复制代码
git branch -d feature-branch

7.4 清理未跟踪的文件

bash 复制代码
git clean -f

删除工作目录中未跟踪的文件。

总结

以上是 Git 常用操作命令的详细说明和示例。这些命令可以帮助你在日常的版本控制工作中更高效地管理代码。

复制代码
相关推荐
. . . . .几秒前
git-ai 项目详解
人工智能·git
New_Horizons6663 小时前
Git 设置命令的别名(alias)
git
恋喵大鲤鱼3 小时前
git stash
git·git stash
M--Y3 小时前
Git原理与使用
git
原来是猿4 小时前
Git【多人协作一】
git
AlbertS4 小时前
记一次推送lfs失败不能迁移git仓库到新的gitlab的问题
git·ai·gitlab·lfs·rejected
Eternity_GQM5 小时前
【Git入门】
大数据·git·elasticsearch
kikikidult7 小时前
GitHub的使用(二)——一次完整 Git 上线流程记录
git·github
茉莉玫瑰花茶7 小时前
Redis 持久化
redis·git·github
KaneLogger17 小时前
OpenCode 操作手册
git