git基本使用

本文最后更新于:2023年3月15日 中午

1 Git常用命令

Git常用的是以下6个命令:

  • git clone
  • git push
  • git add
  • git commit
  • git checkout
  • git pull

这6个命令的关系如下图所示:

说明:

  • workspace:工作区,就是电脑里能看到的目录。
  • staging area:暂存区/缓存区,一般存放在.git目录下的index文件中,所以有时也叫作索引(index)。
  • local repository:版本库或本地仓库
  • remote repository:远程仓库

2 创建仓库

2.1 git init

Git 使用git init命令来初始化一个 Git 仓库,Git 的很多命令都需要在 Git 的仓库中运行,所以git init是使用 Git 的第一个命令。

在执行完成git init命令后,Git 仓库会生成一个 .git 目录,该目录包含了资源的所有元数据,其他的项目目录保持不变。

使用方法:

使用当前目录作为 Git 仓库,我们只需使它初始化。

1
git init

该命令执行完后会在当前目录生成一个 .git 目录。

使用我们指定目录作为Git仓库。

1
git init newrepo

初始化后,会在 newrepo 目录下会出现一个名为 .git 的目录,所有 Git 需要的数据和资源都存放在这个目录中。

2.2 git clone

我们使用git clone从现有 Git 仓库中拷贝项目。

克隆仓库的命令格式为:

1
git clone <repo>

如果我们需要克隆到指定的目录,可以使用以下命令格式:

1
git clone <repo> <directory>

参数说明:

  • repo:Git 仓库。
  • directory:本地目录。

2.3 git config

2.3.1 显示当前的 git 配置信息

1
git config --list

2.3.2 编辑 git 配置文件

1
git config -e    # 针对当前仓库 

或者:

1
git config -e --global   # 针对系统上所有仓库

2.3.3 设置提交代码时的用户信息

1
2
git config --global user.name "runoob"
git config --global user.email test@runoob.com

如果去掉--global参数只对当前仓库有效。

2.4 提交修改

1
2
3
git add . # 将所有更改添加到暂存区 
git commit -m "" # 提交
git push #推送到远程仓库,如有

3 分支操作

3.1 创建分支

1
git branch (branchName)

3.2 切换分支

1
git checkout (branchName)

使用如下命令创建新分支并切换到该新分支下:

1
git checkout -b (branchName)

3.3 删除分支

1
git branch -d (branchName)

3.4 合并分支

1
git merge (branchName)

将任意分支合并到当前分支下。

参考文章:Git 教程 | 菜鸟教程 (runoob.com)


git基本使用
https://summersong.top/post/a531cc26.html
作者
SummerSong
发布于
2022年9月1日
更新于
2023年3月15日
许可协议