變基 Git 分支

John Wachira 2022年4月22日
變基 Git 分支

本教程介紹了使用 git fetchgit rebasegit push 命令將本地分支變基到遠端主分支可以採取的各種步驟。

將本地分支變基為 Git 中的遠端主分支

  1. 獲取更改

    我們使用 git fetch 命令從遠端倉庫中獲取所有更改。

    pc@JOHN MINGW64 ~/Git (Branch1)
    $ git fetch
    remote: Enumerating objects: 3, done.
    remote: Counting objects: 100% (3/3), done.
    remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
    Unpacking objects: 100% (3/3), 610 bytes | 3.00 KiB/s, done.
    From https://github.com/Wachira11ke/Delftscopetech
     * [new branch]      main       -> origin/main
    
  1. 進行修改

    我們執行 git rebase 命令將更改整合到我們的分支。下面的示例將從主分支重新定位我們當前的分支。

    pc@JOHN MINGW64 ~/Git (main)
    $ git rebase main
    Current branch main is up to date.
    

    發生衝突時,請使用 git add . 命令來解決它們。不要在 git add. 之後執行 git commit 命令。命令。

    解決衝突後,使用 git rebase --continue 完成該過程。如果你想在解決衝突後中止程序,請使用 git rebase --abort 命令。

  2. 將更改推送到遠端倉庫

    要在本地倉庫中上傳內容,請使用 git push -f 命令,如下圖所示。

    ``bash
    git push main HEAD -f

    
    `-f` 將覆蓋其他開發人員在遠端倉庫中所做的任何更改。
    
    以下是將更改推送到遠端倉庫的更安全方法。
    
    ``bash
    git push --force-with-lease main HEAD
    

    此命令不會覆蓋遠端倉庫中其他開發人員所做的更改。

    變基和合並用於將更改從一個分支整合到另一個分支。變基是在不干擾分支歷史記錄的情況下更新功能分支的過程,允許更清晰的提交歷史記錄。

    它與 git merge 相反,它在與他人共享時會建立衝突的分支。

作者: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

相關文章 - Git Rebase