Try   HackMD

git 應用指令紀錄

tags: git

Copyright 2021, 月下麒麟 YMont


名詞解釋

定義

  • master:本地分支名
  • origin master:origin 表示遠端,master 表示分支名,接在 origin 之後表示是遠端分支名
  • origin/master:遠端分支在本地的拷貝,因此稱為本地分支

範例

  • git fetch origin master:取得遠端分支 master 的資料
  • git merge origin/master:合併本地分支 origin/master 的程式碼
  • git push origin master:將本地提交紀錄推至遠端分支 master

正常流程

git clone

git clone https://...

git init

Initialized empty Git repository in
D:/1_project/4.CM/.git/

git add
修改檔案或是資料夾,就可以把變更紀錄存起來

git add .

git status

git status

有被git add抓到的變更就會出現綠色字(Changes to be committed)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

沒有被git add抓到的,會出現紅色字(Changes not staged for committed)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →


git commit

git commit -m "your describe"

git remote

git remote add origin http://192.168.xxx.xxx:xxxx/project/main.git

git push

git push origin master

成功的提示訊息 如下

Enumerating objects: 11, done.
Counting objects: 100% (11/11), done.
Delta compression using up to 8 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (10/10), 54.87 KiB | 13.72 MiB/s, done.
Total 10 (delta 0), reused 4 (delta 0), pack-reused 0
remote: . Processing 1 references
remote: Processed 1 references in total
To http://192.168.xxx.xxx:xxxx/project/main.git
   6d144e7..503c0f0  master -> master


除錯解法

git rm remote
錯誤描述

git remote add origin http://192.168.xxx.xxx:xxxx/project/main.git
error: remote origin already exists.

解決方法

git remote rm origin

rebase

git pull --rebase

or

git pull --rebase origin master

rm .git

rm -rf .git

reference:How to upload a project to Github



git pull 與 git pull rebase差異

git pull = git fetch + git merge against tracking upstream branch

git pull rebase = git fetch + git rebase against tracking upstream branch

if you want to know how git merge and git rebase differ, following this.

  • Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  • Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  • Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  • Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

Reference:Git: 四種將分支與主線同步的方法



重新pull & 再次commit

Debug1

新的一天要git pull,從遠端到本地,但是一直失敗
Command
git pull origin master
Error
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.
Debug
表示沒有連結到遠端的repository
git remote add origin http://192.168. ...

Debug2

因本地與遠端的內容不同,需要合併
Command
git pull --rebase
Error
error: cannot pull with rebase: You have unstaged changes.
error: please commit or stash them.
Debug
git add .

Debug3

Command
接著再試一次 >> git pull rebase
git pull --rebase
Error
error: cannot pull with rebase: Your index contains uncommitted changes.
error: please commit or stash them.
Debug
所以要commit才可以
git commit -m "descriptation

reference:git-如何解决git pull之后报错
reference:How to merge remote master to local branch