Git常用指令使用

摘要:之前代码管理都是借助于fork、sourceTree等图形工具,最近发现直接用命令也好用,就总结Git常用的指令

1、Git的介绍

1.1 git官网

安装: Git - Downloading Packagehttps://git-scm.com/download/mac Mac上安装,直接使用,并使用查看版本,验证安装成功

bash 复制代码
czh12@czh12deiMac ~ % brew install git
Running `brew update --auto-update`...
Error: Failed to download https://formulae.brew.sh/api/formula.jws.json!
Failed to download https://formulae.brew.sh/api/cask.jws.json!
==> Downloading https://formulae.brew.sh/api/formula.jws.json
######################################################################## 100.0%
==> Downloading https://formulae.brew.sh/api/cask.jws.json
-=O=-                                          #     #    #    #              
curl: (28) Operation too slow. Less than 100 bytes/sec transferred the last 5 seconds
Error: Failure while executing; `/usr/bin/env /opt/homebrew/Library/Homebrew/shims/shared/curl --disable --cookie /dev/null --globoff --user-agent Homebrew/4.0.11-93-g367fe53\ \(Macintosh\;\ arm64\ Mac\ OS\ X\ 11.6\)\ curl/7.64.1 --header Accept-Language:\ en --fail --progress-bar --location --remote-time --output /Users/czh12/Library/Caches/Homebrew/api/cask.jws.json --compressed --speed-limit 100 --speed-time 5 --progress-bar https://formulae.brew.sh/api/cask.jws.json` exited with 28. Here's the output:
-=O=-                                          #     #    #    #              
curl: (28) Operation too slow. Less than 100 bytes/sec transferred the last 5 seconds

==> Downloading https://formulae.brew.sh/api/cask.jws.json
######################################################################## 100.0%
==> Fetching dependencies for git: gettext and pcre2
==> Fetching gettext
==> Downloading https://raw.githubusercontent.com/Homebrew/homebrew-core/6b9f22a
-=O=-                                     #     #     #    #                  
curl: (22) The requested URL returned error: 404 
Error: git: Failed to download resource "gettext--ruby-source"
Download failed: https://raw.githubusercontent.com/Homebrew/homebrew-core/6b9f22abafb887533d9dc5dd06f604b52ba7c24e/Formula/gettext.rb
czh12@czh12deiMac ~ % git -v
unknown option: -v
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]
czh12@czh12deiMac ~ % git --version
git version 2.30.1 (Apple Git-130)

1.2 重要概念

Git、GitHub 和 GitLab 的区别

  1. Git

    Git 是一个分布式版本控制系统,用于跟踪文件的变化并协助多人协作开发项目。开发者可以通过 Git 轻松地创建分支、提交更改、合并代码等操作,从而有效地管理代码版本。Git 允许每个开发者在本地拷贝完整的代码仓库,并且可以独立工作,无需依赖中央服务器。

  2. GitHub

    GitHub 是一个基于 Git 的代码托管平台,提供了代码仓库托管、团队协作、问题追踪、持续集成等功能。开发者可以在 GitHub 上创建公开或私有的代码仓库,与团队成员共享代码,进行代码审查,管理项目等。GitHub 是一个社交化的平台,开发者可以在上面交流、学习和分享代码。

  3. GitLab

    GitLab 也是一个基于 Git 的代码托管平台,类似于 GitHub,但提供了更多的功能和服务 。除了代码托管外,GitLab 还包括持续集成、部署管道、代码审查、事务管理等功能。GitLab 可以作为自托管的解决方案,企业可以在自己的服务器上搭建 GitLab 实例,更好地控制数据和安全性

四个区域

工作区→暂存区→本地仓库→远端仓库

  1. 工作区(Working Directory)

    工作区是指存放项目源文件的目录,是开发者直接编辑和修改代码的地方。在工作区中进行的修改不会被 Git 跟踪或记录。

  2. 暂存区(Staging Area)

    暂存区是一个中间区域,用于临时存放已经修改但还未提交到本地仓库的更改。开发者可以通过将工作区的修改内容暂存到暂存区,然后一次性提交到本地仓库。

  3. 本地仓库(Local Repository)

    本地仓库是存放项目完整历史记录和版本信息的地方,包含所有提交的快照和元数据。当开发者提交(commit)更改时,这些更改会被永久保存到本地仓库中。

  4. 远端仓库(Remote Repository)

    远端仓库是分布式版本控制系统中的远程存储库,通常托管在云端或其他服务器上。开发者可以将本地仓库中的更改推送(push)到远端仓库,或者从远端仓库拉取(pull)最新的更改到本地仓库。

HEAD:当前所在位置 在 Git 中,HEAD 是一个指向当前所在位置的符号引用。它可以指向当前所在的分支(通常是最新提交的快照)或直接指向特定的提交。 HEAD 通常用于表示当前工作目录所基于的提交版本,也可以用来切换分支、查看历史记录等操作。

master:主分支

在 Git 中,master 是默认的主分支名称,新建仓库时通常会自动创建这个分支。 开发者可以在 master 分支上进行主要的开发工作,也可以根据需要创建其他分支进行功能开发或修复。

branch:分支

分支是 Git 中用于独立开发某个功能或修复某个问题的机制。通过创建分支,可以在不影响主线开发的情况下进行并行开发。 每个分支包含自己的提交历史,可以随时切换不同的分支进行工作。

origin:远端

在 Git 中,origin 是默认的远端仓库名称,通常指向项目在远程服务器上的中央仓库。 当克隆一个远程仓库时,Git 会自动为远程仓库创建一个名为 origin 的别名,方便开发者与远程仓库进行交互。

2、单人开发常用Git命令

2.1 git 配置

配置的生效范围,全局、系统和本地的Git配置文件。例如:

git config --global:使用全局配置文件(用户层面)

git config --system:使用系统配置文件(系统层面)

git config --local:使用仓库配置文件(本地项目)

bash 复制代码
czh12@czh12deiMac ~ % git config --global user.name 'czh12'
czh12@czh12deiMac ~ % git config --global user.email 'czh12@163.com'
czh12@czh12deiMac ~ % git config --get user.name
czh12
czh12@czh12deiMac ~ % git config --get user.email
czh12@163.com

2.2 创建仓库实现初次提交

初始化git仓库

czh12@czh12deiMac ~ % cd /Users/czh12/Desktop/git 
czh12@czh12deiMac git % pwd
/Users/czh12/Desktop/git
czh12@czh12deiMac git % ls
imooc_git
czh12@czh12deiMac git % git init imooc_git
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint:   git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint:   git branch -m <name>
Initialized empty Git repository in /Users/czh12/Desktop/git/imooc_git/.git/

注:查看隐藏文件夹 shift +command + .

git对文件的增删改查

add添加到暂存区:git add

commit 提交到本地仓库: git commit

status 查看状态: git status

bash 复制代码
czh12@czh12deiMac git % ls
imooc_git
czh12@czh12deiMac git % cd imooc_git
czh12@czh12deiMac imooc_git % ls          
month.txt
**## add添加到暂存区:git add 文件名
czh12@czh12deiMac imooc_git % git add month.txt
czh12@czh12deiMac imooc_git % git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
  new file:   month.txt
## commit 提交到本地仓库:  git commit
czh12@czh12deiMac imooc_git % git commit
[master (root-commit) 054f99a] first commit
 1 file changed, 4 insertions(+)
 create mode 100755 month.txt
czh12@czh12deiMac imooc_git % git status
On branch master
nothing to commit, working tree clean
## 修改文件后未上传到暂存区, status 查看状态: git status
czh12@czh12deiMac imooc_git % git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
  modified:   month.txt

no changes added to commit (use "git add" and/or "git commit -a")
czh12@czh12deiMac imooc_git % git add month.txt
czh12@czh12deiMac imooc_git % git status       
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
  modified:   month.txt
## 一步完成提交到暂存区和本地仓库
czh12@czh12deiMac imooc_git % git commit -a
[master cf664e3] commit -a
 1 file changed, 3 insertions(+)

rm 移除文件:git rm 文件名

mv 重命名文件:git mv 原名 改后名称

bash 复制代码
czh12@czh12deiMac imooc_git % git add month2.txt
czh12@czh12deiMac imooc_git % git commit 
[master f350d57] month2.txt
 1 file changed, 7 insertions(+)
 create mode 100755 month2.txt
**## 移除文件:git rm 文件名**
czh12@czh12deiMac imooc_git % git rm month2.txt
rm 'month2.txt'
czh12@czh12deiMac imooc_git % git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
  deleted:    month2.txt

czh12@czh12deiMac imooc_git % git commit
[master d6080b7] remove month2.txt
 1 file changed, 7 deletions(-)
 delete mode 100755 month2.txt
czh12@czh12deiMac imooc_git % git status
On branch master
nothing to commit, working tree clean
## 重命名文件:git mv 原名  改后名称 
czh12@czh12deiMac imooc_git % git mv month.txt  monthname.txt
czh12@czh12deiMac imooc_git % git commit
[master 524d2ce] rename month.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename month.txt => monthname.txt (100%)

git log help 看所有选项

git log -n 限定log个数

git log ---oneline 单行简洁模式

git log ---stat 查看提交历史以及每次提交所引入的更改的统计信息

git log --author='提交者' 按提交者查询

git log --grep='关键字' 按照关键字搜索提交记录

bash 复制代码
czh12@czh12deiMac imooc_git % git log --help
## git log -n
czh12@czh12deiMac imooc_git % git log -1
commit 524d2ce4e9757710202e5d8b117eb7dc52212fd3 (HEAD -> master)
Author: czh12 <czh12@163.com>
Date:   Mon Mar 25 15:35:28 2024 +0800

    rename month.txt
czh12@czh12deiMac imooc_git % git log --oneline
524d2ce (HEAD -> master) rename month.txt
d6080b7 remove month2.txt
f350d57 month2.txt
cf664e3 commit -a
054f99a first commit
czh12@zh12deiMac imooc_git % git log --stat
commit 524d2ce4e9757710202e5d8b117eb7dc52212fd3 (HEAD -> master)
Author: czh12 <czh12@163.com>
Date:   Mon Mar 25 15:35:28 2024 +0800

    rename month.txt

 month.txt => monthname.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)

czh12@czh12deiMac imooc_git % git log --author='czh12'
commit 524d2ce4e9757710202e5d8b117eb7dc52212fd3 (HEAD -> master)
Author: czh12 <czh12@163.com>
Date:   Mon Mar 25 15:35:28 2024 +0800

    rename month.txt

czh12@czh12deiMac imooc_git % git log --grep='month'
commit 524d2ce4e9757710202e5d8b117eb7dc52212fd3 (HEAD -> master)
Author: czh12 <czh12@163.com>
Date:   Mon Mar 25 15:35:28 2024 +0800

    rename month.txt

commit d6080b76aeadba837132ac6ccee67cfc129b4d45
Author: czh12 <czh12@163.com>
Date:   Mon Mar 25 15:33:15 2024 +0800

    remove month2.txt

commit f350d57a52c64cc6467b8c6841f494e441711541
Author: czh12 <czh12@163.com>
Date:   Mon Mar 25 15:31:15 2024 +0800

    month2.txt

2.3 git的图形化界面

learnGitBranching

Learn Git Branchinghttps://learngitbranching.js.org/?locale=zh_CN&NODEMO=

gitk安装:brew install git

2.4 分支Branch

创建分支:git branch 分支名

切换分支:git checkout 分支名

bash 复制代码
czh12@czh12deiMac imooc_git % git branch imoocbranch1
czh12@czh12deiMac imooc_git % git checkout imoocbranch1
Switched to branch 'imoocbranch1'
## 创建分支并切换
czh12@czh12deiMac imooc_git % git checkout -b  imoocbranch2
Switched to a new branch 'imoocbranch2'

2.5 其他使用场景

git reset 撤销工作目录中的更改、回退到之前的提交或者修改提交历史

git reset --hard <commit-hash> :撤销所有从指定提交之后的更改,包括暂存区的更改

git reset --hard HEAD^ : ^号的个数为回退步数

git reset --hard HEAD~n :n为回退步数

场景:撤销最近的提交,可使用偏移符号:^和~

bash 复制代码
## 回退时使用:^的个数表示回退的提交数,~n中n表示回退的提交数
czh12@czh12deiMac imooc_git % git reset --hard HEAD^
HEAD is now at d6080b7 remove month2.txt
czh12@czh12deiMac imooc_git % git reset --hard HEAD~2
HEAD is now at cf664e3 commit -a

git rebase: 变基,会改变原有分支的路径,将一系列提交到一个新的基础提交上

git rebase main :将imooc分支上原有提交,重新提交到main分支(下图中'表示变基过)

合并commit

git reset --soft <commit-hash> : 当前分支的 HEAD 指针移动到指定的提交(commit-hash),同时保留工作目录(working directory)和暂存区(staging area)中的更改。再重新提交,将多次commit合并提交。

场景: 清理提交历史,移除不必要的提交或者合并提交;也可以撤销最近的提交再重新提(git reset --soft HEAD^)

bash 复制代码
czh12@czh12deiMac imooc_git % git log -4   
commit 9cd46ac5fa118a83ef86365eb1a394086e692442 (HEAD -> master)
Author: czh12 <czh12@163.com>
Date:   Mon Mar 25 17:20:23 2024 +0800

    jul

commit 985a67b124381309110b526d355ec4465172823d
Author: czh12 <czh12@163.com>
Date:   Mon Mar 25 17:17:57 2024 +0800

    jun

commit fa6d3d1bdd85dd5961d7392612ef9e5bd0b437aa
Author: czh12 <czh12@163.com>
Date:   Mon Mar 25 17:16:51 2024 +0800

    may

commit **cf664e39f3bf1**fe7347e2c30ce3662171f395510
Author: czh12 <czh12@163.com>
Date:   Mon Mar 25 11:37:27 2024 +0800

    commit -a
czh12@czh12deiMac imooc_git % **git reset --soft ****cf664e39f3bf1fe**
czh12@czh12deiMac imooc_git % git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
  modified:   month.txt

czh12@czh12deiMac imooc_git % git add monthname.txt
fatal: pathspec 'monthname.txt' did not match any files
czh12@czh12deiMac imooc_git % git add month.txt    
czh12@czh12deiMac imooc_git % git commit
[master 2c71f6e] may june july
 1 file changed, 6 insertions(+)
czh12@czh12deiMac imooc_git % git log -3
commit 2c71f6e8b83f2b25c7c3f41437ad4876e77f325d (HEAD -> master)
Author: czh12 <czh12@163.com>
Date:   Mon Mar 25 17:34:01 2024 +0800

    **may june july**

commit cf664e39f3bf1fe7347e2c30ce3662171f395510
Author: czh12 <czh12@163.com>
Date:   Mon Mar 25 11:37:27 2024 +0800

    commit -a

commit 054f99a8e676a6727d49e31270e05e4e8c2c9fc7
Author: czh12 <czh12@163.com>
Date:   Mon Mar 25 11:29:59 2024 +0800

    first commit

修改commit

git commit --amend 修复的意思,但只能修改最新的一次提交

bash 复制代码
czh12@czh12deiMac imooc_git % git add month.txt 
czh12@czh12deiMac imooc_git %  git commit --amend
[master 5f84ec5] may june july Augest
 Date: Mon Mar 25 17:34:01 2024 +0800
 1 file changed, 6 insertions(+)
czh12@czh12deiMac imooc_git % git log -1         
commit 5f84ec5c1c59f87983753fc492a86d6e0c91d2fc (HEAD -> master)
Author: czh12 <czh12@163.com>
Date:   Mon Mar 25 17:34:01 2024 +0800

    **may june july Augest**

修改之前某次指定的提交

git rebase -i <commit-hash>

然后选择edit, 编辑指定的提交

bash 复制代码
czh12@czh12deiMac imooc_git % git rebase -i cf664e39f3bf1fe734
Stopped at 5f84ec5...  may june july Augest
You can amend the commit now, with

  git commit --amend   ## 提示后续步骤

Once you are satisfied with your changes, run

  git rebase --continue   ## 提示后续步骤
  
## 此处选择edit
edit 5f84ec5 may june july Augest
pick 2c7995d Sep
pick 7e7ba3a Oct

# Rebase cf664e3..7e7ba3a onto cf664e3 (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
~                                                                                                                                                                        
~ 

czh12@czh12deiMac imooc_git % git add month.txt
czh12@czh12deiMac imooc_git % git commit --amend
[detached HEAD 4d9c942] may June july Augest
 Date: Mon Mar 25 17:34:01 2024 +0800
 1 file changed, 6 insertions(+)
czh12@zh12deiMac imooc_git % git rebase --continue
Successfully rebased and updated refs/heads/master.

撤销操作

撤销当前分支的变化

场景:切换到错误的分支,并提交了多次,但提交的代码是依然需要的

git reset --soft commitID 回退代码,但依然保存提交内容

git stash 储藏代码

git commit 提交代码

上述操作会将多次提交合并为一次提交

3. 团队开发中常用Git命令

未完待续......

相关推荐
真的很上进8 小时前
【Git必看系列】—— Git巨好用的神器之git stash篇
java·前端·javascript·数据结构·git·react.js
小小安于乱8 小时前
git 更换远程地址的方法
git
FutureUniant9 小时前
GitHub每日最火火火项目(9.21)
人工智能·计算机视觉·ai·github·音视频
高林雨露11 小时前
git 空项目初次提交项目命令记录
git
liu83418944712 小时前
git 本地分支误删,怎么恢复?误删本地已提交未推送的分支!
git
+码农快讯+12 小时前
Git clone远程仓库没有其他分支的问题
git
多恩Stone13 小时前
【Hugging Face 下载中断】Git LFS 如何下载指定文件、单个文件夹?
git·stable diffusion·huggingface·diffusers
佚明zj14 小时前
如何配置ssh key 到gitlab, 实现git push
git·ssh·gitlab
半糖112215 小时前
git配置SSH
git·ssh
zaza0_015 小时前
【github】.gitignore不能忽略一些文件的时候
github