Git基本操作(4)

Git

Github開案

至個人Github Repositories New一個新的repo


開案完畫面顯示如下,紅箭頭是用來本地連結repo的


這裡就直接複製貼上Github提供的建立方法

注意新版Github default branch為main,不是master
git remote add origin https://github.com/EideWang/Find-A-Coach.git
git branch -M main
git push -u origin main


第一行內的' origin '是個代名詞,用來代表後面那串(Github repo),所以是可以改的


第二行新建一個名為main的分支,-M為強制改(命)名(-m是改名,遇到重複的branch name會不能改)


第三行針對 origin推送main這個分支的內容,如果origin(Github repo)上沒有main這個分支,則建立main;有的話則推到最新的位置


-u為 --set-upstream 的縮寫


按下Enter後執行過程的訊息..

Enumerating objects: 139, done.
Counting objects: 100% (139/139), done.
Delta compression using up to 4 threads
Compressing objects: 100% (124/124), done.
Writing objects: 100% (139/139), 145.81 KiB | 2.56 MiB/s, done.
Total 139 (delta 44), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (44/44), done.
To https://github.com/EideWang/Find-A-Coach.git
 * [new branch]   main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.



常見錯誤處理

! [rejected]    main -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/EideWang/Find-A-Coach.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.


看到上面的錯誤訊息,嘗試使用git pull

PS E:\前端學習\Vue Practice\project-find-a-coach> git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

  git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

  git branch --set-upstream-to=origin/<branch> main

表示本地的main跟Github的main分支之間沒有建立好關係


使用git branch -vv確認當前各branch的狀況,下面可看出main沒綁遠端repo

PS E:\前端學習\Vue Practice\project-find-a-coach> git branch -vv
* main  a4d7a2d add a GithubTest file to test push
 master e6f11a7 Revert "Merge branch 'test'" -m 1


git branch --set-upstream-to=origin/REMOTE_BRANCH LOCAL_BRANCH
PS E:\前端學習\Vue Practice\project-find-a-coach> git branch --set-upstream-to=origin/main main
Branch 'main' set up to track remote branch 'main' from 'origin'.


此時再察看一次branch狀態

PS E:\前端學習\Vue Practice\project-find-a-coach> git branch -vv
* main  a4d7a2d [origin/main: ahead 1, behind 1] add a GithubTest file to test push
 master e6f11a7 Revert "Merge branch 'test'" -m 1

main有正確的track origin/main了!!


重試一次push發現一樣跳上面的error訊息,所以先git pull更新本地main branch

PS E:\前端學習\Vue Practice\project-find-a-coach> git push -u origin main
To https://github.com/EideWang/Find-A-Coach.git
 ! [rejected]    main -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/EideWang/Find-A-Coach.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
PS E:\前端學習\Vue Practice\project-find-a-coach> git pull
Removing src/version.txt
Merge made by the 'recursive' strategy.
 .gitignore            | 2 +-
 src/components/ui/BaseButton.vue | 73 ++++++++++++++++++++++++++++++++++++++++
 src/version.txt         | 9 -----
 3 files changed, 74 insertions(+), 10 deletions(-)
 create mode 100644 src/components/ui/BaseButton.vue
 delete mode 100644 src/version.txt


pull下來後,tree結構如下


pull成功後,再次push,將GithubTest.txt更新上Github main branch

PS E:\前端學習\Vue Practice\project-find-a-coach> git pull
Removing src/version.txt
Merge made by the 'recursive' strategy.
 .gitignore            | 2 +-
 src/components/ui/BaseButton.vue | 73 ++++++++++++++++++++++++++++++++++++++++
 src/version.txt         | 9 -----
 3 files changed, 74 insertions(+), 10 deletions(-)
 create mode 100644 src/components/ui/BaseButton.vue
 delete mode 100644 src/version.txt
PS E:\前端學習\Vue Practice\project-find-a-coach> git push -u origin main
Enumerating objects: 11, done.
Counting objects: 100% (11/11), done.
Delta compression using up to 4 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (7/7), 700 bytes | 700.00 KiB/s, done.
Total 7 (delta 4), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (4/4), completed with 3 local objects.
To https://github.com/EideWang/Find-A-Coach.git
  8523b1e..c352baa main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.


至此便完成了Github版本上傳的任務了!👏👏👏


© 2021 Hamsterism. All rights reserved github