Git系列用教程

奋斗吧
奋斗吧
擅长邻域:未填写

标签: Git系列用教程 博客 51CTO博客

2023-07-12 18:24:22 35浏览

Git系列用教程,git是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目1.预备内容1.1工作区、暂存区、(本地、远程)版本库1.2相关工具2.常用命令tip:githelp可以快速打开帮助文档2.1gitinit(初始化)初始化本地仓库,默认会创建一个master分支arilen.lv@klmnbrd100MINGW64/e$mkdirgit_share&&a

git是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目

1.预备内容

1.1 工作区、暂存区、(本地、远程)版本库

Git系列用教程_暂存区

1.2 相关工具

Git系列用教程_工作区_02

2.常用命令

tip: git help 可以快速打开帮助文档

2.1 git init(初始化)

初始化本地仓库,默认会创建一个master分支

arilen.lv@klmnbrd100 MINGW64 /e
$ mkdir git_share && cd ./git_share

arilen.lv@klmnbrd100 MINGW64 /e/git_share
$ git init
Initialized empty Git repository in E:/git_share/.git/

arilen.lv@klmnbrd100 MINGW64 /e/git_share (master)
2.2 git config(配置)

查看、设置配置信息

查看配置参数: git config --list或git config -l

$ git config -l
diff.astextplain.textconv=astextplain
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
http.sslbackend=openssl
http.sslcainfo=D:/git/Git/mingw64/ssl/certs/ca-bundle.crt
core.autocrlf=true
core.fscache=true
core.symlinks=false
pull.rebase=false
credential.helper=manager-core
credential.https://dev.azure.com.usehttppath=true
init.defaultbranch=master
user.name=lvsheng
user.email=arilen.lv@kalerm.com
core.autocrlf=true
core.excludesfile=C:\Users\arilen.lv\Documents\gitignore_global.txt
alias.st=status
alias.co=checkout
credential.https://gitee.com.provider=generic
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true

设置配置参数: git config --system --local --global

--local 仓库级别(优先级最高)-只对该仓库生效 --global 用户级别 (优先级次之)-推荐设置 --system 系统级别(优先级最低)

arilen.lv@klmnbrd100 MINGW64 /e/git_share (master)
$ git config --global user.name lvsheng

arilen.lv@klmnbrd100 MINGW64 /e/git_share (master)
$ git config --global user.email arilen.lv@kalerm.com

arilen.lv@klmnbrd100 MINGW64 /e/git_share (master)
$ git config -l
diff.astextplain.textconv=astextplain
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
http.sslbackend=openssl
http.sslcainfo=D:/git/Git/mingw64/ssl/certs/ca-bundle.crt
core.autocrlf=true
core.fscache=true
core.symlinks=false
pull.rebase=false
credential.helper=manager-core
credential.https://dev.azure.com.usehttppath=true
init.defaultbranch=master
user.name=lvsheng
user.email=arilen.lv@kalerm.com
core.autocrlf=true
core.excludesfile=C:\Users\arilen.lv\Documents\gitignore_global.txt
alias.st=status
alias.co=checkout
credential.https://gitee.com.provider=generic
core.repositoryformatversion=0
core.filemode=false
core.bare=false
core.logallrefupdates=true
core.symlinks=false
core.ignorecase=true
- $ git config --global alias.co checkout
- $ git config --global alias.br branch
- $ git config --global alias.ci commit
- $ git config --global alias.st status
2.3 git status(状态)

查看工作区、暂存区文件的状态

Git系列用教程_工作区_03

2.4 git add(添加)

将文件从工作区添加到暂存区

添加一个或多个文件到暂存区:git add [file1] [file2] ... 添加指定目录到暂存区,包括子目录:git add [dir] 添加当前目录下的所有文件到暂存区:git add .

arilen.lv@klmnbrd100 MINGW64 /e/git_share (master)
$ git add example.txt
warning: in the working copy of 'example.txt', LF will be replaced by CRLF the next time Git touches it
arilen.lv@klmnbrd100 MINGW64 /e/git_share (master)
$ git status
On branch master
No commits yet
Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   example.txt
2.5 git rm(移除)

将文件从工作区或暂存区删除

从暂存区和工作区中删除文件:git rm

git rm example.txt

删除的文件处于暂存区,加上-f 即可(标识强制删除): git rm -f

git rm -f example.txt

将文件从暂存区移除到工作区,加上--cached 选项即可:git rm --cached

git rm --cached example.txt
2.6 git checkout(检出、切换)

新建并切换分支: git checkout -b (branch name) 本质上是git branch (branch name)和git checkout (branch name)组合命令 切换分支: git checkout (branch name) 忽略工作区文件的修改: 放弃对某个文件的修改: git checkout -- file path name 放弃对所有文件的修改:git checkout .

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ echo 'a' >> checkout.txt

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git status
On branch develop
Your branch is ahead of 'origin/develop' by 1 commit.
  (use "git push" to publish your local commits)

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:   checkout.txt

no changes added to commit (use "git add" and/or "git commit -a")
g
arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git checkout checkout.txt
Updated 1 path from the index

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git status
On branch develop
Your branch is ahead of 'origin/develop' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git checkout -b test
Switched to a new branch 'test'

arilen.lv@klmnbrd100 MINGW64 /e/git_share (test)

arilen.lv@klmnbrd100 MINGW64 /e/git_share (test)
$ git checkout develop
Switched to branch 'develop'
Your branch is ahead of 'origin/develop' by 1 commit.
  (use "git push" to publish your local commits)

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
2.7 git commit(提交)

将文件从工作区或暂存区添加到版本库

将暂存区内容提交到版本库并并附注提交信息: git commit -m [message] 将工作区已被跟踪的文件更改提交到版本库: git commit -am [message](本质上是git add和git commit组合命令) 修改上一次commit的提交信息: git commit --amend -m [message]

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ echo ‘b’ >> checkout.txt

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git status
On branch develop
Your branch is ahead of 'origin/develop' by 1 commit.
  (use "git push" to publish your local commits)

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:   checkout.txt

no changes added to commit (use "git add" and/or "git commit -a")
g
arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git commit -am 'add b'
warning: in the working copy of 'checkout.txt', LF will be replaced by CRLF the next time Git touches it
[develop c3f27dc] add b
 1 file changed, 1 insertion(+)
g
arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git status
On branch develop
Your branch is ahead of 'origin/develop' by 2 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
2.8 git branch(分支)

查询、创建、删除分支

查询所有分支: git branch 新建分支: git branch [branch name] 删除分支: git branch -d branch name 强制删除分支: git branch -D branch name 查看本地分支与远程分支的关系: git branch -vv

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git branch
* develop
  master
  test

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git branch release

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git branch
* develop
  master
  release
  test

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git branch -d release
Deleted branch release (was c3f27dc).

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git branch
* develop
  master
  test

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git branch release

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ echo 'c' >> checkout.txt

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git status
On branch develop
Your branch is ahead of 'origin/develop' by 2 commits.
  (use "git push" to publish your local commits)

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:   checkout.txt

no changes added to commit (use "git add" and/or "git commit -a")

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git checkout release
Switched to branch 'release'
M       checkout.txt

arilen.lv@klmnbrd100 MINGW64 /e/git_share (release)
$ git status
On branch release
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:   checkout.txt

no changes added to commit (use "git add" and/or "git commit -a")
g
arilen.lv@klmnbrd100 MINGW64 /e/git_share (release)
$ git commit -am 'add c'
warning: in the working copy of 'checkout.txt', LF will be replaced by CRLF the next time Git touches it
[release d9fd017] add c
 1 file changed, 1 insertion(+)

arilen.lv@klmnbrd100 MINGW64 /e/git_share (release)
$ git checkout develop
Switched to branch 'develop'
Your branch is ahead of 'origin/develop' by 2 commits.
  (use "git push" to publish your local commits)
g
arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git branch -d release
error: The branch 'release' is not fully merged.
If you are sure you want to delete it, run 'git branch -D release'.

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git branch -D release
Deleted branch release (was d9fd017).

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git branch
* develop
  master
  test
2.9 git merge(合并)

用于分支合并,将其他分支变更信息合并到本分支上

fast-forward合并演示:

合并前分支情况:

Git系列用教程_git_04

arilen.lv@klmnbrd100 MINGW64 /e/git_share (test)
$ git merge develop
Updating e3cd079..51118c8
Fast-forward
 checkout.txt | 1 +
 1 file changed, 1 insertion(+)

合并后分支情况:

Git系列用教程_工作区_05

可以看出合并后并没有产生新的提交节点,命令行响应也提示此次合并属于fast-foward,由此可见git默认会启用fast-forward合并。

合并时加上-- no-ff参数会禁用fast-forward,这样会多出一个commit id

合并前分支情况:

Git系列用教程_git_06

arilen.lv@klmnbrd100 MINGW64 /e/git_share (test)
$ git merge --no-ff develop
Merge made by the 'ort' strategy.
 example.txt | 1 +
 1 file changed, 1 insertion(+)

合并后分支情况:

Git系列用教程_暂存区_07

可以看出此次合并属于fast-foward,但是仍然会多出一个commit节点

非fast-forward合并分支演示:

合并前分支情况:

Git系列用教程_工作区_08

arilen.lv@klmnbrd100 MINGW64 /e/git_share (test)
$ git merge develop
Updating bbcee98..1cfcf85
Fast-forward
 checkout.txt | 3 +++
 1 file changed, 3 insertions(+)

合并后分支情况:

Git系列用教程_git_09

可以看出合并后,发生冲突需要解决冲突,并不属于fast-forword,因此会多出一个提交节点

2.10 git log(日志)

查看提交历史

查看最近n条的提交记录: git log -n 、git log ---pretty=oneline 、git log -n --pretty=format:"%h - %an,%ar:%s"

$ git log
commit c3f27dc25cad011339288d3821b2a8fbb42690f2 (HEAD -> develop)
Author: lvsheng <arilen.lv@kalerm.com>
Date:   Thu Apr 13 12:58:59 2023 +0800

    add b

commit bbcee98b7fc839e1e641927b040049067fa84304 (test)
Author: lvsheng <arilen.lv@kalerm.com>
Date:   Thu Apr 13 12:41:24 2023 +0800

    add checkout file

commit 39860a2e17ddf212ec79524790999f22b3c042a6 (tag: v2, tag: v1.1, tag: v1.0, origin/develop)
Author: lvsheng <arilen.lv@kalerm.com>
Date:   Wed Apr 12 11:17:12 2023 +0800

    add world

commit 2316561e18082939317c57c90dd2f1835d4581b7 (origin/master, origin/HEAD, master)
Author: lvsheng <arilen.lv@kalerm.com>
Date:   Tue Apr 11 11:49:51 2023 +0800

    1 commit

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git log -2
commit c3f27dc25cad011339288d3821b2a8fbb42690f2 (HEAD -> develop)
Author: lvsheng <arilen.lv@kalerm.com>
Date:   Thu Apr 13 12:58:59 2023 +0800

    add b

commit bbcee98b7fc839e1e641927b040049067fa84304 (test)
Author: lvsheng <arilen.lv@kalerm.com>
Date:   Thu Apr 13 12:41:24 2023 +0800

    add checkout file

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git log -2 --pretty=format:"%h -%an,%ar:%s"
c3f27dc -lvsheng,5 minutes ago:add b
bbcee98 -lvsheng,23 minutes ago:add checkout file

idea内置功能:

Git系列用教程_工作区_10

2.11 git stash(暂存)

场景: 正在某一个分支下做需求,还未完成时就需要在另外一个分支下进行问题的修改,如果直接checkout切换分支会有冲突,会导致切换不成功,但是又不想将当前的做的修改丢弃,想将当前的改动暂存下来,然后切换到另外一个分支去修改问题,当问题修改完成了之后将分支切换回来,然后再将之前暂存的改动恢复继续对需求进行开发

添加暂存文件: git stash -m 'message' 查看暂存列表: git stash list 删除暂存: git stash drop stash@{index} 应用最近一次保存的内容,并将该次保存的内容出栈(即将栈顶出栈,释放最近一次保存的内容后将该次保存内容删除): git stash pop 应用某一次的存储内容,但不删除改内容(即该存储的内容还是存储在栈内): git stash apply** **stash@{index}

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ echo 'd' >> checkout.txt

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git status
On branch develop
Your branch is ahead of 'origin/develop' by 2 commits.
  (use "git push" to publish your local commits)

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:   checkout.txt

no changes added to commit (use "git add" and/or "git commit -a")
g
arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git stash -m 'add d'
warning: in the working copy of 'checkout.txt', LF will be replaced by CRLF the next time Git touches it
Saved working directory and index state On develop: add d

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git stash list
stash@{0}: On develop: add d
g
arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ echo 'e' >> checkout.txt

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git stash -m 'add e'
warning: in the working copy of 'checkout.txt', LF will be replaced by CRLF the next time Git touches it
Saved working directory and index state On develop: add e
g
arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git stash list
stash@{0}: On develop: add e
stash@{1}: On develop: add d

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git stash apply stash@{1}
On branch develop
Your branch is ahead of 'origin/develop' by 2 commits.
  (use "git push" to publish your local commits)

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:   checkout.txt

no changes added to commit (use "git add" and/or "git commit -a")

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git stash list
stash@{0}: On develop: add e
stash@{1}: On develop: add d
arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git stash drop stash@{0}
Dropped stash@{0} (4ee2f5c0534faf5bc21928f6c3333888dd2aa289)

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git stash list
stash@{0}: On develop: add d
2.12 git reset(重置)

资源版本回滚

回滚所有内容到上一次提交: git reset HEAD^ 回滚某个文件到上一次提交: git reset HEAD^ filename 回滚到某一次指定的提交: git reset sha1-value

tip: 上述回滚操作均会将中间提交节点的信息保留在工作区,如需全部丢弃可加上--hard参数,撤销工作区中所有未提交的修改内容并将暂存区与工作区都回到上一次版本,并删除之前的所有信息提交

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git log -3
commit 2fb6ab5493dadc511f371283f71cfdea746f8a37 (HEAD -> develop)
Author: lvsheng <arilen.lv@kalerm.com>
Date:   Thu Apr 13 13:13:40 2023 +0800

    add e

commit f04f94dd33164eb78c9c70908240436090a98b28
Author: lvsheng <arilen.lv@kalerm.com>
Date:   Thu Apr 13 13:12:31 2023 +0800

    add d

commit c3f27dc25cad011339288d3821b2a8fbb42690f2
Author: lvsheng <arilen.lv@kalerm.com>
Date:   Thu Apr 13 12:58:59 2023 +0800

    add b

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git reset --hard HEAD^
HEAD is now at f04f94d add d

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git log -3
commit f04f94dd33164eb78c9c70908240436090a98b28 (HEAD -> develop)
Author: lvsheng <arilen.lv@kalerm.com>
Date:   Thu Apr 13 13:12:31 2023 +0800

    add d

commit c3f27dc25cad011339288d3821b2a8fbb42690f2
Author: lvsheng <arilen.lv@kalerm.com>
Date:   Thu Apr 13 12:58:59 2023 +0800

    add b

commit bbcee98b7fc839e1e641927b040049067fa84304 (test)
Author: lvsheng <arilen.lv@kalerm.com>
Date:   Thu Apr 13 12:41:24 2023 +0800

    add checkout file
2.13 git clone(克隆)

从远程仓库拉取资源到本地(完整的版本库)

arilen.lv@klmnbrd100 MINGW64 /e
$ git clone https://gitee.com/harry7/git_share.git
Cloning into 'git_share'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
2.14 git fetch(获取)

拉取当前分支关联的远程分支的最新资源到本地的远程仓库副本,并不会与当前分支合并

Git系列用教程_git_11

远程仓库副本,可以理解为存在于本地的远程仓库缓存. 如需更新,可通过git fetch/pull命令获取远程仓库内容.使用fech获取时,并未合并到本地仓库,此时可使用git merge实现远程仓库副本与本地仓库的合并.

tip: 通常在执行git fetch之后紧接着执行git merge远程分支到你所在的任意分支,推荐使用git pull命令进行远程分支拉取操作

2.15 git pull(拉取)

拉取当前分支关联的远程分支的最新资源到本地,并于当前分支合并

git pull 本质上是git fetch和git merge组合命令完整命令是git pull origin remote-branch:local-branch

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ echo 'f' >> checkout.txt

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git commit -am 'add f'
warning: in the working copy of 'checkout.txt', LF will be replaced by CRLF the next time Git touches it
[develop 1cfcf85] add f
 1 file changed, 1 insertion(+)
g
arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git push origin develop:develop
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 303 bytes | 303.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
To https://gitee.com/harry7/git_share.git
   f04f94d..1cfcf85  develop -> develop

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git push
Everything up-to-date
2.16 git push(推送)

推送当前分支提交的资源到远程的关联分支上

git push 完整命令是git push origin local-branch:remote-branch

arilen.lv@klmnbrd100 MINGW64 /e/git_share (master)
# 切换到develop分支
$ git checkout develop
Switched to branch 'develop'
M       example.txt
arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
# 状态查看
$ git status
On branch develop
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:   example.txt

no changes added to commit (use "git add" and/or "git commit -a")
# 提交更改
arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git commit -am 'add world'
warning: in the working copy of 'example.txt', LF will be replaced by CRLF the next time Git touches it
[develop 39860a2] add world
 1 file changed, 1 insertion(+)
g
arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
# 推送到远程(提示失败 因为此时develop分支不)
$ git push
fatal: The current branch develop has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin develop

To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git push origin develop:develop
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 252 bytes | 252.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-6.4]
remote: Create a pull request for 'develop' on Gitee by visiting:
remote:     https://gitee.com/harry7/git_share/pull/new/harry7:develop...harry7:master
To https://gitee.com/harry7/git_share.git
 * [new branch]      develop -> develop

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git branch -vv
* develop 39860a2 add world
  master  2316561 [origin/master] 1 commit

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git push --set-upstream origin develop
Everything up-to-date
branch 'develop' set up to track 'origin/develop'.

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git branch -vv
* develop 39860a2 [origin/develop] add world
  master  2316561 [origin/master] 1 commit
2.17 git tag(标签)

发布一个版本时,我们通常先在版本库中打一个标签(tag),这样就唯一确定了打标签时刻的版本。将来无论什么时候,取某个标签的版本,就是把那个打标签的时刻的历史版本取出来。所以,标签也是版本库的一个快照。标签分为轻量级标签(lightweight)、附注标签(annotated)

创建轻量级标签: git tag tag-name

git tag v1.0.1

创建附注标签: git tag -a tag-name -m 'description message'

git tag -a v1.2 -m 'release version'

删除标签: git tag -d tag-name

git tag -d v1.0.1

修改标签: git tag new-tag-name old-tag-name

git tag  v1.0.2 v1.0.1

查看标签列表: git tag查找标签: git tag -l 'pattern'

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git tag
v1.0
v1.1
v2

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git tag -l 'v1.0'
v1.0

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git tag -l 'v1*'
v1.0
v1.1

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git tag -l '*1*'
v1.0
v1.1

推送单个tag:git push origin [tagname] git push origin local-tag-name:remote-tag-name

git push origin v1.0

推送多个tag: git push origin [tagname] [tagname]

git push origin v2.0 v3.0

推送所有tag: git push --tags或git push origin --tags(将本地尚未推送到远程的tag推送到远程服务器)

2.18 git cherry pick(局部复制粘贴)

merge命令会合并所有的提交,这个命令是将其他分支上的commit选择性地合并到当前分支

合并某分支的某一次提交记录:git cherry-pick commitid合并某分支的某几次提交记录:git cherry-pick commitid1 commitid2 ... 合并某分支的commitid1到commitid2之间的提交记录,但不包含commitid1的这次提交记录:git cherry-pick commitid1..commitid2 合并某分支的commitid1到commitid2之间的所有的提交记录,包含commitid1的这次提交记录: git cherry-pick commitid1^..commitid2 合并某分支上最新的一次提交记录:git cherry-pick branch-name

cherry-pick时出现冲突该如何解决?继续执行cherry-pick: 用户手动先手动解决有冲突的文件,然后将文件重新加入暂存区(git add),第二步使用如下命令让cherry-pick继续执行 git cherry-pick --continue 取消cherry-pick: 如果认为冲突太多了,不想继续cherry-pick,那么可以使用如下命令取消cherry-pick,这个时候会恢复到cherry-pick前的样子 git cherry-pick --abort 发生代码冲突后,退出 cherry pick,但是不回到操作前的样子: git cherry-pick --quit

cherry-pick前:

Git系列用教程_暂存区_12

arilen.lv@klmnbrd100 MINGW64 /e/git_share (develop)
$ git co test
Switched to branch 'test'

arilen.lv@klmnbrd100 MINGW64 /e/git_share (test)
$ git cherry-pick 6fc058a
[test 3c06ffe] add u
 Date: Thu Apr 13 16:20:30 2023 +0800
 1 file changed, 1 insertion(+)
2.19 git blame

追溯一个指定文件的历史修改记录

查看指定文件的修改历史: git blame filename使用-L指定文件的行数范围: git blame -L n1,n2 filename

arilen.lv@klmnbrd100 MINGW64 /e/git_share (master)
$ git blame checkout.txt
bbcee98b (lvsheng 2023-04-13 12:41:24 +0800 1) checkout example
c3f27dc2 (lvsheng 2023-04-13 12:58:59 +0800 2) ‘b’
f04f94dd (lvsheng 2023-04-13 13:12:31 +0800 3) d
1cfcf854 (lvsheng 2023-04-13 13:47:32 +0800 4) f
84170906 (lvsheng 2023-04-13 14:07:50 +0800 5) g
3114c8a0 (lvsheng 2023-04-13 14:08:15 +0800 6) h
776d5cb8 (lvsheng 2023-04-13 14:18:59 +0800 7) i
51118c8c (lvsheng 2023-04-13 14:26:30 +0800 8) k

arilen.lv@klmnbrd100 MINGW64 /e/git_share (master)
$ git blame -L 1,4 checkout.txt
bbcee98b (lvsheng 2023-04-13 12:41:24 +0800 1) checkout example
c3f27dc2 (lvsheng 2023-04-13 12:58:59 +0800 2) ‘b’
f04f94dd (lvsheng 2023-04-13 13:12:31 +0800 3) d
1cfcf854 (lvsheng 2023-04-13 13:47:32 +0800 4) f

idea内置功能:

Git系列用教程_工作区_13

2.20 .gitignore

忽略文件并避免跟踪

配置语法:/: 开头表示目录 *: 通配多个字符 ?: 通配单个字符 !: 表示不忽略(跟踪)匹配到的文件或目录 具体参考: git help gitignore

.git
logs
rebel.xml
target/
*.idea
*.iws
*.iml
*.ipr

3. 补充点

3.1 HEAD和origin:

HEAD: 当前活跃分支的游标,在.git目录下存在HEAD文件,该文件内部不包含SHA-1值commit-id而是一个指向另外一个引用的指针

arilen.lv@klmnbrd100 MINGW64 /e/git_share (test)
$ cat .git/HEAD
ref: refs/heads/test

当执行commit命令的时候,git会创建一个commit对象,并且会将这个commit对象的parent指针设置为HEAD所指向的引用SHA-1值origin: 远程仓库链接的别名,只不过仓库链接太长了,直接写链接多不方便,于是就搞了个标记,用它来代表它就是那个仓库链接

参考链接: https://juejin.cn/post/6844903493078089736

3.2 git merge和git rebase区别:

参考链接: https://www.cnblogs.com/michael-xiang/p/13179837.html

3.3 gitLab搭建:

参考链接: https://blog.csdn.net/qq_15290209/article/details/126230624

好博客就要一起分享哦!分享海报

此处可发布评论

评论(0展开评论

暂无评论,快来写一下吧

展开评论

您可能感兴趣的博客

客服QQ 1913284695