美文网首页
git 使用

git 使用

作者: pingxhcn | 来源:发表于2022-01-05 15:05 被阅读0次

    背景

    rebase 使用

    1、 切换到子分支,rebase 主分支到子分支
    git rebase master(主分支name)
    2、如果有冲突,解决完冲突使用
    git add .
    git rebase --continue
    3、rebase 成功后,会提示 落后多少版本 ,最后强制推送到远程
    git push -f origin 当前分支name
    

    强制推送到远端就可以了。

    git 子模块使用:
    如果未对子模块进行更改,但是在pull 代码的时候,显示出子模块的地址,使用命令更新一下子模块

    // 更新到初始化版本
    git submodule update --init
    或
    // 更新到最新版本
    git submodule update --remote
    

    git revert 回滚

    回滚到历史某一次提交,新开子分支,以防数据丢失

    git revert -n [commitId]
    手动解决可能出现的冲突
    git commit -m 'commit info'
    git push 
    

    开发过程中,本地通常会有无数次 commit ,可以合并“相同功能”的多个 commit,以保持历史的简洁。

    git rebase 合并多条 commit

    从HEAD版本开始往过去数3个版本
    $ git rebase -i HEAD~3
    
    指定要 rebase 节点的 commitId;
    如果子分支多条commit 要合并到 一条 commit , 要用子分支的根节点 commitId
    $ git rebase -i [commitid]
    

    说明:

    -i(--interactive):弹出交互式的界面进行编辑合并
    [commitid]:要合并多个版本之前的版本号,注意:[commitid] 本身不参与合并
    

    指令解释(交互编辑时使用):

    p, pick = use commit
    r, reword = use commit, but edit the commit message
    e, edit = use commit, but stop for amending
    s, squash = use commit, but meld into previous commit
    f, fixup = like "squash", but discard this commit's log message
    x, exec = run command (the rest of the line) using shell
    d, drop = remove commit
    

    合并步骤

    查看 log 记录,使用git rebase -i选择要合并的 commit
    编辑要合并的版本信息,保存提交,多条合并会出现多次(可能会出现冲突)
    修改注释信息后,保存提交,多条合并会出现多次
    推送远程仓库或合并到主干分支
    查看 log

    291e427 update website
    8c8f3f4 update website
    1693a6f update clear-logs.sh version
    3759b84 update clear-logs.sh
    fc36a2a add links
    1d795e6 fix && update clear-logs.sh 0.0.2
    9536dab add dingtalk script
    3a51aaa fix shellcheck problem
    2db6ad3 add clear logs scripts
    e57b0e6 fix && add batch del
    17cb931 fix && add batch del
    cf7e875 add redis script
    fe4bbcb Initial commit```
    编辑要合并版本
    
    ```# 指定要合并版本号,cf7e875 不参与合并,进入 vi 编辑器
    $ git rebase -i cf7e875 
    pick 17cb931 fix && add batch del
    pick e57b0e6 fix && add batch del
    pick 2db6ad3 add clear logs scripts
    pick 3a51aaa fix shellcheck problem
    pick 9536dab add dingtalk script
    pick 1d795e6 fix && update clear-logs.sh 0.0.2
    pick fc36a2a add links
    pick 3759b84 update clear-logs.sh
    pick 1693a6f update clear-logs.sh version
    pick 8c8f3f4 update website
    
    # Rebase cf7e875..291e427 onto cf7e875 (10 commands)
    #
    # Commands:
    # p, pick = use commit
    # r, reword = use commit, but edit the commit message
    # e, edit = use commit, but stop for amending
    # s, squash = use commit, but meld into previous commit
    # f, fixup = like "squash", but discard this commit's log message
    # x, exec = run command (the rest of the line) using shell
    # d, drop = remove commit
    #
    # 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.
    #
    # Note that empty commits are commented out
    将 commit 内容编辑如下:
    
    # 把 e57b0e6 合并到 17cb931,不保留注释;把 1693a6f 合并到 3759b84
    pick 17cb931 fix && add batch del
    f e57b0e6 fix && add batch del
    pick 2db6ad3 add clear logs scripts
    pick 3a51aaa fix shellcheck problem
    pick 9536dab add dingtalk script
    pick 1d795e6 fix && update clear-logs.sh 0.0.2
    pick fc36a2a add links
    pick 3759b84 update clear-logs.sh
    s 1693a6f update clear-logs.sh version
    pick 8c8f3f4 update website
    然后:wq保存退出后是注释界面:
    
    # This is a combination of 2 commits.
    # This is the 1st commit message:
    
    update clear-logs.sh
    
    # This is the commit message #2:
    
    update clear-logs.sh version
    
    # Please enter the commit message for your changes. Lines starting
    # with '#' will be ignored, and an empty message aborts the commit.
    #
    # Date:      Tue Jul 28 18:25:57 2020 +0800
    #
    # interactive rebase in progress; onto cf7e875
    # Last commands done (9 commands done):
    #    pick 3759b84 update clear-logs.sh
    #    s 1693a6f update clear-logs.sh version
    # Next command to do (1 remaining command):
    #    pick 8c8f3f4 update website
    # You are currently editing a commit while rebasing branch 'master' on 'cf7e875'.
    #
    # Changes to be committed:
    # modified:   logs/README.md
    # modified:   logs/clear-logs.sh
    #
    

    编辑注释信息,保存退出:wq即可完成 commit 的合并:

    update clear-logs.sh
    查看合并后的 log

    $ git log --oneline
    47e7751 update website
    4c2316c update clear-logs.sh
    73f082e add links
    56adcf2 fix && update clear-logs.sh 0.0.2
    ebf3786 add dingtalk script
    6e81ea7 fix shellcheck problem
    64ca58f add clear logs scripts
    9327def fix && add batch del
    cf7e875 add redis script
    fe4bbcb Initial commit
    推送远程

    # 由于我是一个人开发,故强制推送到远程仓库
    $ git push --force origin master
    

    冲突解决

    在 git rebase 过程中,可能会存在冲突,此时就需要解决冲突。

    错误提示信息:git rebase -i resumeerror: could not apply ...。

    查看冲突

    $ git status

    解决冲突之后,本地提交

    $ git add .

    rebase 继续

    $ git rebase --continue

    rebase

    切换到要 rebase 的分支
    例: 将 master 主分支 rebase 到 dev子分支
    切换到 dev 子分支 ,右击 master ,选中"将当前变更变基到master"

    git rebase 误操作导致文件丢失撤销并恢复文件

    第一步
    执行 git reflog 查看本地操作记录

    找到本次rebase之前的操作id 例如:89356d0

    第二步 执行恢复命令
    git reset --hard 89356d0

    有出现提示的话,则输入y确认

    忽略文件无效

    在Sourcetree 的偏好设置中,加入git 的忽略文件没有效果


    image.png

    设置项目的忽略文件如下
    打开Sourcetree中的项目,点击右上角的 设置 按钮

    image.png

    高级 中编辑忽略文件

    image.png

    点击确定完成
    打开项目终端 , 输入以下指令即可

    git add .
    git commit -m "refresh .gitignore file"
    

    如果忽略文件失效,则需要先清除缓存

    git rm -r .cached .
    //然后再输入以下指令
    git add .
    git commit -m "refresh .gitignore file"
    

    相关文章

      网友评论

          本文标题:git 使用

          本文链接:https://www.haomeiwen.com/subject/shdcvktx.html