Git笔记

389 阅读6分钟

本文除了git基础命令和操作之外,更多侧重于自己在实际操作中遇到的问题

git:开源的分布式版本控制系统

1689425580467.png

1689423645061.png

  • workspace:工作区
  • staging area:暂存区/缓存区(index)
  • local repository:版本库或本地仓库
  • remote repository:远程仓库

git基本操作

命令说明
git init初始化仓库
git clone 项目地址拷贝一份远程仓库,也就是下载一个项目
git add添加文件到暂存区
命令说明
git status查看仓库当前的状态,显示有变更的文件(红的是没加入暂存区 add之后文件变为绿色)
git log查看历史提交记录
git diff比较文件的不同,即暂存区和工作区的差异。
git (文件) commit -m "注释"提交暂存区到本地仓库 git commit -a = git add + git commit -m "" (绿色:没commit 灰色:commit了)
git reset回退版本。
git rm将文件从暂存区和工作区中删除。
git mv移动或重命名工作区文件
分支命令说明
git branch列出所有分支
git checkout 分支名切换分支
git branch 分支名/git checkout -b 分支名创建分支
git branch -d 分支名删除分支
git merge 分支名合并分支
git pullgit pull=git fetch+git merge FETCH_HEAD
git pushgit push origin master=git push origin master:master

merge出现冲突可以选择手动修改,修改之后使用git add告诉GIT冲突已解决

之前处理过一次冲突解决步骤如下:
vi a.txt          #解决冲突的文件
git add  a.txt    #将修复的文件添加至暂存区,这一步必须执行,否则无法执行下一步操作
git merge --continue  #继续执行pull操作中的merge
git pull ==== git fetch + git merge
参考博文:https://blog.csdn.net/qq_41496495/article/details/124610907
​
又看到一种解决方案是(我没试过):
$ git status -s
UU runoob.php
$ git add runoob.php
$ git status -s
M  runoob.php
$ git commit
[master 88afe0e] Merge branch 'change_site'

git reset

git reset 回退版本 git reset [--soft|--mixed|--hard] [HEAD]

参数说明示例
mixed默认为该参数,可以不带。用于重置暂存区的文件与上一次的提交(commit)保持一致,工作区保持不变git reset [HEAD]
soft用于回退到某个版本git reset --soft HEAD
hard撤销工作区中所有未提交的修改内容,将暂存区与工作区都回到上一次版本,并删除之前的所有信息提交git reset --hard HEAD
参数详细示例
mixedgit reset HEAD^ 回退所有内容到上一个版本 git reset HEAD^ hello.txt 回退hello.txt文件的版本到上一个版本 git reset 052e 回退到指定版本
softgit reset --soft HEAD~3 回退到上上上一个版本
hardgit reset --hard HEAD~3 回退到上上上一个版本 git reset --hard bae128 回退到某个版本回退点之前的所有信息 git reset --hard origin/master 将本地的状态回退到和远程的一样

HEAD说明

表示当前版本HEADHEAD~0
上一个版本HEAD^HEAD~1
上上一个版本HEAD^^HEAD~2
上上上一个版本HEAD^^^HEAD~3
...

git stash

当前分支做的修改不想提交,git stash可将修改的内容保存至堆栈区,待合适的时候恢复。如果在错误的分支进行了开发,可以先将内容保存至堆栈区,在目标分支上恢复。

命令说明
git stash可以将工作区和暂存区所有未提交的修改保存至堆栈区
git stash save "注释"用法同 git stash,如果使用 git stash会产生一条名为" WIP on branchname …"的内容,使用 git stash save 可以添加描述信息
git stash list查看 stash 栈
git stash pop应用 stash 会恢复之前缓存的工作目录,这个指令将缓存堆栈中的第一个 stash 删除(栈是先进后出)
git stash apply <stash>应用某一个stash 内容,不删除记录,默认是应用栈顶(即最新)的一条 stash
git stash drop [<stash>]默认删除栈顶stash 内容 例: git stash drop stash@{0}
git stash clear清空 stash 堆栈
git stash show显示 stash 的修改内容

默认情况下,git stash 会缓存下列文件:

  • 添加到暂存区的修改(staged changes)
  • Git 跟踪的但并未添加到暂存区的修改(unstaged changes)

但不会缓存以下文件:

  • 在工作目录中新的文件(untracked files)
  • 被忽略的文件(ignored files)

git stash 命令提供了参数用于缓存上面两种类型的文件。

  • 使用 -u 或者 --include-untracked 可以 stash untracked 文件。
  • 使用 -a 或者 --all 命令可以 stash 当前目录下的所有修改。

git rebase

参考文章:blog.csdn.net/weixin_4231…

HEAD

HEAD 是当前分支引用的指针,它总是指向某次commit,默认是上一次的commit。 这表示 HEAD 将是下一次提交的父结点。 通常,可以把 HEAD 看做你的上一次提交的快照。当然HEAD的指向是可以改变的,比如你提交了commit,切换了仓库,分支,或者回滚了版本,切换了tag等 原文链接:blog.csdn.net/raoxiaoya/a…

遇到的示例

本地master出现错误 删除本地master
​
git rebase --abort
​
git reset --hard HEAD 
​
git reset --hard HEAD^^^
​
git pull -r origin master 
fatal: It seems that there is already a rebase-merge directory, and
I wonder if you are in the middle of another rebase.  If that is the
case, please try
        git rebase (--continue | --abort | --skip)
If that is not the case, please
        rm -fr ".git/rebase-merge"
and run me again.  I am stopping in case you still have something
valuable there.
Found a swap file by the name "/d/quantinfotech/rates-pm/rates-pm-datamodel/.git/.COMMIT_EDITMSG.swp"
          owned by: 86189   dated: Thu Jul 13 15:32:08 2023
         file name: /d/quantinfotech/rates-pm/rates-pm-datamodel/.git/COMMIT_EDITMSG
          modified: YES
         user name: 86189   host name: WangWenjing
        process ID: 1546
While opening file "/d/quantinfotech/rates-pm/rates-pm-datamodel/.git/COMMIT_EDITMSG"
             dated: Fri Jul 14 10:12:56 2023
      NEWER than swap file!
​
(1) Another program may be editing the same file.  If this is the case,
    be careful not to end up with two different instances of the same
    file when making changes.  Quit, or continue with caution.
(2) An edit session for this file crashed.
    If this is the case, use ":recover" or "vim -r /d/quantinfotech/rates-pm/rates-pm-datamodel/.git/COMMIT_EDITMSG"
    to recover the changes (see ":help recovery").
    If you did this already, delete the swap file "/d/quantinfotech/rates-pm/rates-pm-datamodel/.git/.COMMIT_EDITMSG.swp"
​
    to avoid this message.
​
Swap file "/d/quantinfotech/rates-pm/rates-pm-datamodel/.git/.COMMIT_EDITMSG.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# interactive rebase in progress; onto c5f874c
# Last commands done (2 commands done):
#    pick 9b915f1 交易通道补充的datamodel
#    pick de6c837 EVT-15912
# Next command to do (1 remaining command):
#    pick 58f0fb5 EVT-15912
# You are currently rebasing branch 'EVT-15912' on 'c5f874c'.
#
# Changes to be committed:
#       modified:   src/main/java/com/xxx/xxx/xxx.java
#       modified:   src/main/java/com/src/main/java/com/xxx/xxx/xxxx.java
#       modified:   src/main/java/com/src/main/java/com/xxx/xxx/xxxxx.java
...

1689479292444.png