Git 常用命令

Git常用命令

1. 设置用户名

  1. 基本语法

    git config --global user.name 用户名
    git config --global user.email 邮箱

  2. 案例实操作

全局范围的签名设置:

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git config --global user.name Layne 
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git config --global user.email Layne@atguigu.com 
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) 
$ cat ~/.gitconfig
[user]
   name = Layne
   email = Layne@atguigu.com

说明: 签名的作用是区分不同操作者身份。用户的签名信息在每一个版本的提交信息中能够看
到,以此确认本次提交是谁做的。Git 首次安装必须设置一下用户签名,否则无法提交代码。 ※注意:这里设置用户签名和将来登录 GitHub(或其他代码托管中心)的账号没有任
何关系。

2. 初始化本地库

  1. 基本语法

    git init

  2. 案例实操

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720
$ git init
Initialized empty Git repository in D:/Git-Space/SH0720/.git/
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) $ ll -a
total 4
drwxr-xr-x 1 Layne 197609 0 1125 14:07 ./
drwxr-xr-x 1 Layne 197609 0 1125 14:07 ../ 
drwxr-xr-x 1 Layne 197609 0 1125 14:07 .git/  (.git 初始化的效果,生成 git)
  1. 结果查看

在这里插入图片描述

3. 查看本地库状态

  1. 基本语法

    git status

  2. 案例实操

  • 首次查看(工作区没有任何文件)
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) 
$ git status
On branch master
No commits yet
nothing to commit (create/copy files and use "git add" to track)
  • 新增文件(hello.txt)
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) 
$ vim hello.txt
hello git! hello word!
hello git! hello word!
hello git! hello word!
hello git! hello word!
hello git! hello word!
hello git! hello word!
hello git! hello word!
  • 再次查看(检测到未追踪的文件)
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
hello.txt
nothing added to commit but untracked files present (use "git add" to track)

4. 添加暂存区

  • 将工作区的文件添加到暂存区
  1. 基本语法

    git add 文件名

  2. 案例实操

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git add hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory.
  • 查看状态(检测到暂存区有新文件)
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) 
$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
newfile: hello.txt

5. 提交到本地库

  • 将暂存区的文件提交到本地库
  1. 基本语法

    git commit -m “日志信息” 文件名

  2. 案例实操

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git commit -m "my first commit" hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory.
[master (root-commit) 86366fa] my first commit 1 file changed, 16 insertions(+)
create mode 100644 hello.txt
  • 查看状态(没有文件需要提交)
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) $ git status
On branch master
nothing to commit, working tree clean

6. 修改文件(hello.txt)

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) 
$ vim hello.txt
hello git! hello word!    2222222222222
hello git! hello word!
hello git! hello word!
hello git! hello word!
hello git! hello word!
hello git! hello word!
hello git! hello word!
  • 查看状态(检测到工作区有文件被修改)
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) 
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
  • 将修改的文件再次添加暂存区
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) 
$ git add hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory.
  • 查看状态(工作区的修改添加到了暂存区)
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) 
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: hello.txt

7. 历史版本

  • 查看历史版本
  1. 基本语法

    git reflog 查看版本信息
    git log 查看版本详细信息

  2. 案例实操

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git reflog
087a1a7 (HEAD -> master) HEAD@{0}: commit: my third commit ca8ded6 HEAD@{1}: commit: my second commit
86366fa HEAD@{2}: commit (initial): my first commit
  • 版本穿梭
  1. 基本语法

    git reset --hard 版本号

  2. 案例实操

--首先查看当前的历史记录,可以看到当前是在 087a1a7 这个版本 
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git reflog
087a1a7 (HEAD -> master) HEAD@{0}: commit: my third commit 
ca8ded6 HEAD@{1}: commit: my second commit
86366fa HEAD@{2}: commit (initial): my first commit

--切换到 86366fa 版本,也就是我们第一次提交的版本 
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) 
$ git reset --hard 86366fa
HEAD is now at 86366fa my first commit

--切换完毕之后再查看历史记录,当前成功切换到了 86366fa 版本
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git reflog
86366fa (HEAD -> master) HEAD@{0}: reset: moving to 86366fa
087a1a7 HEAD@{1}: commit: my third commit
ca8ded6 HEAD@{2}: commit: my second commit
86366fa (HEAD -> master) HEAD@{3}: commit (initial): my first commit

--然后查看文件 hello.txt,发现文件内容已经变化 $ cat hello.txt
hello git! hello word!
hello git! hello word!
hello git! hello word!
hello git! hello word!
hello git! hello word!
hello git! hello word!
hello git! hello word!

Git 切换版本,底层其实是移动的 HEAD 指针,具体原理如下图所示。
在这里插入图片描述

8. 查看分支

  1. 基本语法

    git branch -v

  2. 案例实操

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) 
$ git branch -v
* master 087a1a7 my third commit (*代表当前所在的分区)

9. 创建分支

  1. 基本语法

    git branch 分支名

  2. 案例实操

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) 
$ git branch hot-fix
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) 
$ git branch -v
hot-fix 087a1a7 my third commit (刚创建的新的分支,并将主分支master
的内容复制了一份)
* master 087a1a7 my third commit

10.修改分支

--在 maste 分支上做修改
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) 
$ vim hello.txt
--添加暂存区
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git add hello.txt --提交本地库
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) 
$ git commit -m "my forth commit" hello.txt
[master f363b4c] my forth commit
1 file changed, 1 insertion(+), 1 deletion(-)
--查看分支
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) 
$ git branch -v
hot-fix 087a1a7 my third commit (hot-fix 分支并未做任何改变)
* master f363b4c my forth commit (当前master分支已更新为最新一次提交 的版本)
--查看 master 分支上的文件内容
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) 
$ cat hello.txt
hello git! hello word!     2222222222222
hello git! hello word!	   3333333333333
hello git! hello word!
hello git! hello word!
hello git! hello word!
hello git! hello word!
hello git! hello word!
hello git! hello word!   master test
hello git! hello word!

11.切换分支

  1. 基本语法

    git checkout 分支名

  2. 案例实操

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) 
$ git checkout hot-fix
Switched to branch 'hot-fix'
--发现当先分支已由 master 改为 hot-fix
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (hot-fix) 
$
--查看 hot-fix 分支上的文件内容发现与 master 分支上的内容不同 
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (hot-fix) 
$ cat hello.txt
hello git! hello word!   2222222222222
hello git! hello word!   3333333333333
hello git! hello word!
hello git! hello word!
hello git! hello word!
hello git! hello word!
hello git! hello word!

--在 hot-fix 分支上做修改
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (hot-fix) 
$ cat hello.txt
hello git! hello word!		2222222222222
hello git! hello word!		3333333333333
hello git! hello word!
hello git! hello word!
hello git! hello word!
hello git! hello word! 
hello git! hello word!  	hot-fix test

--添加暂存区
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (hot-fix) 
$ git add hello.txt
--提交本地库
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (hot-fix) 
$ git commit -m "hot-fix commit" hello.txt

11.分支合并

  1. 基本语法

    git merge 分支名

  2. 案例实操

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master)
$ git merge hot-fix
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
Automatic merge failed; fix conflicts and then commit the result.

11.产生冲突

冲突产生的表现:后面状态为 MERGING

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master|MERGING)
$ cat hello.txt
hello git! hello word!	2222222222222
hello git! hello word!	3333333333333
hello git! hello word!
hello git! hello word!
hello git! hello word!
hello git! hello word!
hello git! hello word!
<<<<<<< HEAD
hello git! hello word!	master test
hello git! hello word!
=======
hello git! hello word!
hello git! hello word! hot-fix test
>>>>>>> hot-fix

冲突产生的原因:
合并分支时,两个分支在同一个文件的同一个位置有两套完全不同的修改。Git 无法替
我们决定使用哪一个。必须人为决定新代码内容。

查看状态(检测到有文件有两处修改)

Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master|MERGING) 
$ git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
bothmodified: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")

11.解决冲突

  1. 编辑有冲突的文件,删除特殊符号,决定要使用的内容
    特殊符号:<<<<<<< HEAD 当前分支的代码 ======= 合并过来的代码 >>>>>>> hot-fix
hello git! hello word! 2222222222222 
hello git! hello word! 3333333333333 
hello git! hello word!
hello git! hello word!
hello git! hello word!
hello git! hello word!  master test
hello git! hello word!	hot-fix test 
  1. 添加到暂存区
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master|MERGING) 
$ git add hello.txt
  1. 执行提交(注意:此时使用 git commit 命令时不能带文件名)
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master|MERGING) 
$ git commit -m "merge hot-fix"
[master 69ff88d] merge hot-fix
--发现后面 MERGING 消失,变为正常
Layne@LAPTOP-Layne MINGW64 /d/Git-Space/SH0720 (master) 
$

12.创建分支和切换分支图解

在这里插入图片描述
master、hot-fix 其实都是指向具体版本记录的指针。当前所在的分支,其实是由 HEAD 决定的。所以创建分支的本质就是多创建一个指针。
HEAD 如果指向 master,那么我们现在就在 master 分支上。 HEAD 如果执行 hotfix,那么我们现在就在 hotfix 分支上。
所以切换分支的本质就是移动 HEAD 指针。

<think>我们被要求生成一个关于Git常用命令的汇总列表。根据用户的要求,我们需要提供结构清晰的回答,并尽量保证真实可靠。同时,我们参考了站内引用中提供的Git命令信息。根据引用内容,我们可以整理出以下常用命令:1.基本操作(初始化、添加、提交等)2.分支管理3.远程仓库操作4.标签管理5.历史查看与撤销6.其他实用命令(如删除大文件、清理仓库等)我们将按照这些类别来组织命令。注意:由于引用中提供了一些特定问题的解决方案(如删除大文件),我们也会将其作为实用命令的一部分。另外,注意用户要求使用中文回答,并且行内数学表达式使用$...$格式(但本问题不涉及数学表达式,所以忽略),独立公式使用$$...$$格式(同样不涉及)。同时,在回答末尾生成相关问题。我们开始整理Git常用命令汇总:</think>### Git常用命令汇总 以下是Git常用命令分类整理,帮助您高效管理代码版本: #### 1. 仓库初始化与基础操作 ```bash # 初始化新仓库 $ git init # 克隆远程仓库 $ git clone <远程仓库URL> # 查看当前状态 $ git status # 添加文件到暂存区 $ git add <文件名> # 添加单个文件 $ git add . # 添加所有修改 # 提交更改 $ git commit -m "提交说明" ``` #### 2. 分支管理 ```bash # 创建分支 $ git branch <新分支名> # 切换分支 $ git checkout <分支名> $ git switch <分支名> # Git 2.23+推荐方式 # 创建并切换分支 $ git checkout -b <新分支名> $ git switch -c <新分支名> # 合并分支 $ git merge <要合并的分支名> # 删除分支 $ git branch -d <分支名> # 安全删除 $ git branch -D <分支名> # 强制删除未合并分支 ``` #### 3. 远程仓库操作 ```bash # 添加远程仓库 $ git remote add origin <远程仓库URL> # 查看远程仓库 $ git remote -v # 推送到远程仓库 $ git push -u origin <分支名> # 首次推送 $ git push # 后续推送 # 拉取远程更新 $ git pull origin <分支名> $ git pull --rebase origin <分支名> # 变基式拉取 # 删除远程连接 $ git remote rm origin ``` #### 4. 版本回退与撤销 ```bash # 查看提交历史 $ git log $ git log --oneline # 简洁版日志 # 撤销工作区修改 $ git checkout -- <文件名> # 撤销暂存区文件 $ git reset HEAD <文件名> # 版本回退 $ git reset --hard <commit_id> # 彻底回退 $ git reset --soft <commit_id> # 保留修改 # 恢复删除的文件 $ git restore <文件名> # Git 2.23+ ``` #### 5. 标签管理 ```bash # 查看标签 $ git tag # 创建标签 $ git tag <标签名> # 轻量标签 $ git tag -a v1.0 -m "版本说明" # 附注标签 # 推送标签 $ git push origin <标签名> # 删除标签 $ git tag -d <标签名> # 本地删除 $ git push origin :refs/tags/<标签名> # 远程删除 ``` #### 6. 高级维护 ```bash # 清理历史大文件 $ git filter-branch --force --index-filter \ 'git rm -rf --cached --ignore-unmatch <文件名>' \ --prune-empty --tag-name-filter cat -- --all # 回收存储空间 $ git reflog expire --expire=now --all $ git gc --prune=now # 查看仓库大小 $ git count-objects -vH ``` #### 7. 配置相关 ```bash # 设置用户名/邮箱 $ git config --global user.name "用户名" $ git config --global user.email "邮箱" # 查看配置 $ git config --list # 设置默认编辑器 $ git config --global core.editor vim ``` > **最佳实践提示**: > 1. 频繁提交小变更,保持提交记录清晰 > 2. 使用`.gitignore`文件排除无需版本控制的文件 > 3. 重要分支(如main/prod)设置保护规则 > 4. 推送前先执行`git pull --rebase`避免合并提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值