發現fork後的專案少了分支


Posted by mijouhsieh on 2024-01-31

chatGTP 解釋

1 指令 git branch -a 確認有無分支


$ git branch -a
* main
  remotes/origin/HEAD -> origin/main
  remotes/origin/main

git branch -a是一個 Git 命令,用於列出本地和遠程(remote)的所有分支。這個命令會顯示所有的分支,包括已合併和未合併的分支
git branch 用於列出本地分支。
-a 選項則表示顯示所有分支,包括遠程分支。


2 git fetch upstream 從原始存儲庫獲取最新的分支資訊。
錯誤表明 Git 無法找到名為 'upstream' 的遠程存儲庫。在 git fetch upstream 中,'upstream' 是一個遠程的名稱,通常用於指向你 fork 的存儲庫的上游(即被 fork 的存儲庫)。

$ git fetch upstream
fatal: 'upstream' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

3 添加了原始存儲庫作為遠程
git remote add upstream <原始存儲庫的URL>


4 git remote -v 檢查已經添加的遠程

$ git remote -v
origin  https://github.com/MiJouHsieh/typescript-challenge.git (fetch)
origin  https://github.com/MiJouHsieh/typescript-challenge.git (push)
upstream        https://github.com/hexschool/typescript-challenge.git (fetch)
upstream        https://github.com/hexschool/typescript-challenge.git (push)

5 git fetch upstream 從原始存儲庫獲取最新的分支資訊。

$ git fetch upstream
remote: Enumerating objects: 33, done.
remote: Counting objects: 100% (33/33), done.
remote: Compressing objects: 100% (33/33), done.
remote: Total 33 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (33/33), 12.37 KiB | 60.00 KiB/s, done.
From https://github.com/hexschool/typescript-challenge
 * [new branch]      finalTask  -> upstream/finalTask
 * [new branch]      main       -> upstream/main

6 git branch -a 確認沒有分支

$ git branch -a
* main
  remotes/origin/HEAD -> origin/main
  remotes/origin/main
  remotes/upstream/finalTask
  remotes/upstream/main

以下解釋

* main
  feature-branch
  remotes/origin/HEAD -> origin/main
  remotes/origin/main
  remotes/origin/feature-branch
  remotes/upstream/main
  remotes/upstream/feature-branch

main 和 feature-branch 是本地分支。
remotes/origin/ 下的分支是遠程分支,origin 是默認的遠程名稱。
remotes/upstream/ 下的分支是另一個遠程分支,upstream 是另一個可能存在的遠程名稱。
這樣的列表提供了所有可用的分支信息,可以幫助你確認哪些分支存在,並選擇在哪個分支進行操作。


7 創建本地分支:
使用以下命令在你的本地存儲庫中創建缺少的分支。例如,如果缺少一個名為 feature-branch 的分支,可以使用:git checkout -b feature-branch upstream/feature-branch
這樣會在你的本地存儲庫中創建 feature-branch 分支,並將其設置為跟蹤 upstream 的 feature-branch。

$ git checkout -b feature-branch upstream/finalTask
Switched to a new branch 'feature-branch'
Branch 'feature-branch' set up to track remote branch 'finalTask' from 'upstream'.

8 在本地修改分支名稱:
git branch -m old-branch-name new-branch-name


9 因為還沒有推送到遠端,推送新分支到你的 fork:git push origin finalTask

 git push origin finalTask
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: Create a pull request for 'finalTask' on GitHub by visiting:
remote:      https://github.com/MiJouHsieh/typescript-challenge/pull/new/finalTask
remote:
To https://github.com/MiJouHsieh/typescript-challenge.git
 * [new branch]      finalTask -> finalTask

9-1 推送修改到遠端:
這將刪除遠端的 old-branch-name 分支並將新分支推送到遠端。在這個命令中,: 的作用是刪除 old-branch-name。
git push origin :old-branch-name new-branch-name

9-2 重命名遠端分支:
如果其他人已經clone了你的存儲庫,他們需要更新他們的本地分支名稱。他們可以使用
git branch -m old-branch-name new-branch-name
然後,他們可以使用以下命令來更新遠端分支:
git fetch origin

注意事項:
請確保你在修改分支名稱之前已經提交了所有的更改,或者你可以使用 git stash 暫存未提交的更改。
如果有其他人正在協作這個分支,請提前通知他們這個更改,以免造成混亂。
請謹慎操作,確保你理解這些命令的影響,並在進行重要更改之前進行備份。


#fork #branch







Related Posts

[ 前端工具 ] - Webpack

[ 前端工具 ] - Webpack

JavaScript 的櫃子,陣列( Array )

JavaScript 的櫃子,陣列( Array )

SQL 檢視表、常用函式與集合運算入門教學

SQL 檢視表、常用函式與集合運算入門教學


Comments