---
# System prepended metadata

title: Git Command Line
tags: [Git, Command Line]

---

# Git Command Line

###### tags: `Git` `Command Line`

# Reference

* [Git Book](https://git-scm.com/book/en/v2)

---

# Frequent use

## ``clone``:
```
git clone <URL>
```

## ``checkout``:
```
git checkout --track <remote>/<branch>
git checkout --track origin/dev_C1

git switch -c <branch> --track <remote>/<branch>
git switch -c dev_v3.0_PSU_temp --track origin/dev_v3.0_PSU_temp

git checkout test01
```

## ``update``:
```
git update-git-for-windows
```

## ``list remote branches``:
```
git branch -r
```

---

## ``status``
```
> git status
On branch test01
Your branch and 'origin/test01' have diverged,
and have 1 and 2 different commits each, respectively.
  (use "git pull" if you want to integrate the remote branch with yours)

nothing to commit, working tree clean
```

### Explain:

your local ``test01`` branch and the ``origin/test01`` remote branch 
have both received new commits that the other doesn't have.

Your branch has 1 different commit: 
You've made a new commit on your local ``test01`` branch and haven't pushed it yet.

'origin/test01' has 2 different commits: 
Someone else has pushed two new commits to the test01 branch on the remote repository, 
and you haven't pulled them yet.

This situation occurs because your local history and 
the remote history have a different set of new commits. 
You need to reconcile these two distinct histories.


## ``log``

```
> git log test01..origin/test01
commit 571de89bd6b21b96ba04ef97217f4ae35dd624d0 (origin/test01)
Author: 林政賢 <elite_lin@tw-futaba.com.tw>
Date:   Mon Sep 8 16:20:00 2025 +0800

    Edit README.md

commit 77aabcca3767357f615c7017bac5692275672768
Merge: 0437dfe 4113607
Author: 林政賢 <elite_lin@tw-futaba.com.tw>
Date:   Mon Sep 8 16:17:06 2025 +0800

    Merge branch 'test01' into 'master'

    edit readme to test1

    See merge request elite/android_ci_test01!3
```

---

## ``fetch``:
```
git -c diff.mnemonicprefix=false -c core.quotepath=false --no-optional-locks fetch --no-tags BitBucket

git -c diff.mnemonicprefix=false -c core.quotepath=false --no-optional-locks fetch --tags origin
```

## ``pull``
```
git pull origin test01

git pull --rebase origin test01
```
    
## ``push``:
```
git -c diff.mnemonicprefix=false -c core.quotepath=false --no-optional-locks push -v --set-upstream BitBucket master:master timer_interrupt:timer_interrupt timer_pwm1:timer_pwm1

git -c diff.mnemonicprefix=false -c core.quotepath=false --no-optional-locks push -v origin test01:test01
``` 

---

# Practice

## 001

```
git checkout --track origin/dev_C1
```

* [Difference between git checkout --track origin/branch and git checkout -b branch origin/branch](https://stackoverflow.com/questions/10002239/difference-between-git-checkout-track-origin-branch-and-git-checkout-b-branch)

```
git checkout -b branch origin/branch
git checkout --track origin/branch
```

The practical difference comes when using a local branch named differently:

``git checkout -b mybranch origin/abranch``
will create mybranch and track origin/abranch
``git checkout --track origin/abranch`` 
will only create 'abranch', not a branch with a different name.
(That is, as commented by Sebastian Graf, if the local branch did not exist already.
If it did, you would need git checkout -B abranch origin/abranch)

Note: with Git 2.23 (Q3 2019), that would use the new command ``git switch``:

```
git switch -c <branch> --track <remote>/<branch>
```

If the branch exists in multiple remotes and 
one of them is named by the checkout.defaultRemote configuration variable, 
we'll use that one for the purposes of disambiguation, 
even if the ``<branch>`` isn't unique across all remotes.
Set it to e.g. checkout.defaultRemote=origin 
to always checkout remote branches from there 
if ``<branch>`` is ambiguous but exists on the 'origin' remote.

## 002 - fetch / pull / rebase

### Step1
```
> git log test01..origin/test01
commit 571de89bd6b21b96ba04ef97217f4ae35dd624d0 (origin/test01)
Author: 林政賢 <elite_lin@tw-futaba.com.tw>
Date:   Mon Sep 8 16:20:00 2025 +0800

    Edit README.md

commit 77aabcca3767357f615c7017bac5692275672768
Merge: 0437dfe 4113607
Author: 林政賢 <elite_lin@tw-futaba.com.tw>
Date:   Mon Sep 8 16:17:06 2025 +0800

    Merge branch 'test01' into 'master'

    edit readme to test1

    See merge request elite/android_ci_test01!3
```

### Step2
```
> git status
On branch test01
Your branch and 'origin/test01' have diverged,
and have 1 and 2 different commits each, respectively.
  (use "git pull" if you want to integrate the remote branch with yours)

nothing to commit, working tree clean
```

### Step3-1 (Option 1. Pull & Merge)

#### 1. Make sure you are on the correct branch:
```
git checkout test01
```

#### 2. Pull the latest changes from the remote and merge them into your branch:
```
git pull origin test01
```

#### 2.1. or Alternatively, you can use git fetch followed by git merge:

```
git fetch origin
git merge origin/test01
```

### Step3-2 (Option 2. Pull & rebase)

#### 1. Make sure you are on the correct branch:
```
git checkout test01
```

#### 2. Rebase your local changes on top of the remote branch:

```
> git checkout test01
Already on 'test01'
Your branch and 'origin/test01' have diverged,
and have 1 and 2 different commits each, respectively.
  (use "git pull" if you want to integrate the remote branch with yours)

> git pull --rebase origin test01
Enter passphrase for key 'C:\Users\elite\.ssh\futaba_gitlab.pem':
From git.tw-futaba.com.tw:elite/android_ci_test01
 * branch            test01     -> FETCH_HEAD
Successfully rebased and updated refs/heads/test01.
```

## 003 - pull


Push example
```
 git -c diff.mnemonicprefix=false -c core.quotepath=false --no-optional-locks push -v origin test01:test01
Pushing to git.tw-futaba.com.tw:elite/android_ci_test01.git
Enter passphrase for key 'C:\Users\elite\.ssh\futaba_gitlab.pem':
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 12 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 458 bytes | 229.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
remote:
remote: To create a merge request for test01, visit:
remote:   https://git.tw-futaba.com.tw/elite/android_ci_test01/-/merge_requests/new?merge_request%5Bsource_branch%5D=test01
remote:
To git.tw-futaba.com.tw:elite/android_ci_test01.git
   571de89..8a934d6  test01 -> test01
updating local tracking ref 'refs/remotes/origin/test01'
```

This means:

* ``571de89``: 
This is the commit ID of the remote branch before the push.
* ``8a934d6``: 
This is the new commit ID of the remote branch after the push.
* ``test01 -> test01``: 
This confirms that your local ``test01`` branch was pushed to the ``test01`` branch on the origin remote.


---
    
# Update Git client

## [Reference](https://stackoverflow.com/questions/13790592/how-to-upgrade-git-on-windows-to-the-latest-version)

Since Git 2.16.1(2) you can use

```
C:\> git update-git-for-windows
```

In version between 2.14.2 and 2.16.1, the command was

```
C:\> git update
```
    
---

# Exercise:


## Initialization

在此之前, 要先建立一個資料夾，並切換到其中

```
git init
```

這一步會產生 ``.git`` 資料夾, 之後就可以加入要被 Track 的檔案, 並進行 ``commit``

## Add file to be tracked:

```
git add <filename>
```

## Commit
```
git commit -m "<human-readable-text-message"
```

以下是我的操作, 假設我已有一個 ``Git_Study`` 資料夾, 
我在裡面建立一個 ``my_project_01`` 資料夾, 用來學習、測試 Git command line

```
D:\>cd Git_Study
D:\Git_Study>cd my_project_01
```

進行 Initialization

```
D:\Git_Study\my_project_01>git init
Initialized empty Git repository in D:/Git_Study/my_project_01/.git/
```

之後我用文字編輯建立2個檔案 ``Program.cs``, ``LICENSE``

``Program.cs``:
```
using System;

namespace HelloDotnet
{
    class Program
    {
        static void Main(string[] args)
        {
        	// added comment #001
            Console.WriteLine("Hello Dotnet!");
        }
    }
}
```

``LICENSE``:
```
Free to use.
```

之後就可以利用 ``git add`` track 這兩個檔案:

```
D:\Git_Study\my_project_01>git add Program.cs
D:\Git_Study\my_project_01>git add LICENSE
```

最後進行第一次 commit:

```
D:\Git_Study\my_project_01>git commit -m "Commit for the very first time"
[master (root-commit) 61b294b] Commit for the very first time
 2 files changed, 13 insertions(+)
 create mode 100644 LICENSE
 create mode 100644 Program.cs
```

---

上述的方式是從零開始建立; 另一種是從遠端再專案 ``clone`` 下來;
在此, 假設已經在 User 資料夾中的 ``.ssh`` 資料夾中設定好金鑰。

首先, 是測試 ssh 連線

## Test connection via SSH
```
D:\Git_Study>ssh -T git@bitbucket.org
Enter passphrase for key 'D:\key_store\my_ssh_key.pem':
logged in as elite_lin

You can use git or hg to connect to Bitbucket. Shell access is disabled
```

之後可以進行 clone 了

## Git Clone

```
git clone <remote-uri>
```

```
D:\>cd Git_Study
D:\Git_Study>git clone git@github.com:vexonelite/csharpstudy.git
Cloning into 'csharpstudy'...
The authenticity of host 'github.com (13.114.40.48)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,13.114.40.48' (RSA) to the list of known hosts.
Enter passphrase for key 'D:\key_store\my_ssh_key.pem':
remote: Enumerating objects: 283, done.
remote: Counting objects: 100% (283/283), done.
remote: Compressing objects: 100% (180/180), done.
Receiving objects:  66% (187/283)   ed 226 (delta 93), pack-reused 0
Receiving objects: 100% (283/283), 52.50 KiB | 746.00 KiB/s, done.
Resolving deltas: 100% (150/150), done.
```


---

回到 ``my_project_01`` 的狀態

## Create a new branch
```
git branch <branch-name>
```

## Switched to branch
```
git checkout <branch-name>
```

建立 branch ``testing1`` 並切換過去
```
D:\Git_Study\my_project_01>git branch testing1

D:\Git_Study\my_project_01>git checkout testing1
Switched to branch 'testing1'
```


    
---

## Git status

此時，編輯 ``Program.cs``, ``LICENSE``, 
然後增加一個 ``Core``資料夾, 並在其中增加一個 ``library.cs`` 檔案;
然後利用 ``git status`` 可以看目前當下有哪些 track 檔案異動

```
D:\Git_Study\my_project_01>git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   LICENSE
        modified:   Program.cs

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        Core/

no changes added to commit (use "git add" and/or "git commit -a")
```

---

此時，有一種情況是想捨棄當下修改過內容的檔案，使其回復到未變動情況;

## Discard / Reset all changes

若是想放棄所有已修改過的檔案, 可以直接使用 

```
git reset --hard
```

```
D:\Git_Study\my_project_01>git reset --hard
HEAD is now at eca9c5b ready to test conflict #001 on master

D:\Git_Study\my_project_01>git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        Core/

nothing added to commit but untracked files present (use "git add" to track)
```

可以看見, ``Program.cs``, ``LICENSE`` 已經直接被還原了!

---

## Discard / Reset a single file:

* [git: revert (reset) a single file](https://www.norbauer.com/rails-consulting/notes/git-revert-reset-a-single-file)

但若僅想還原某一個檔案, 可以利用 

```
git checkout -- <filename>
```



假設我又修改了 ``Program.cs``
```
D:\Git_Study\my_project_01>git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   Program.cs

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        Core/

no changes added to commit (use "git add" and/or "git commit -a")
```

然後還原 ``Program.cs``
```
D:\Git_Study\my_project_01>git checkout -- Program.cs

D:\Git_Study\my_project_01>git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        Core/

nothing added to commit but untracked files present (use "git add" to track)
```

---

接著，又對 ``Program.cs`` 並 commit:

```
D:\Git_Study\my_project_01>git add Program.cs

D:\Git_Study\my_project_01>git commit "ready to test conflict #001"
error: pathspec 'ready to test conflict #001' did not match any file(s) known to git.

D:\Git_Study\my_project_01>git commit -m "ready to test conflict #001"
[testing1 ddf5db0] ready to test conflict #001
 2 files changed, 4 insertions(+), 1 deletion(-)
```

接著, 切回 ``master`` 分支，並對對 ``Program.cs``, ``LICENSE`` 修改, 然後 commit:

```
D:\Git_Study\my_project_01>git add Program.cs

D:\Git_Study\my_project_01>git add LICENSE

D:\Git_Study\my_project_01>git commit -m "ready to test conflict #001 on master"
[master eca9c5b] ready to test conflict #001 on master
 2 files changed, 4 insertions(+), 1 deletion(-)
```


## Commit history

此時可以使用 ``git log`` 可以看過往 commit 紀錄

```
git log --decorate --graph --all

git log --oneline --decorate --graph --all
```

```
D:\Git_Study\my_project_01>git log --decorate --graph --all
* commit eca9c5be565ab1a231ceda316f7469ea1f5b27b3 (HEAD -> master)
| Author: elite_lin <elite_lin@goglobal.com.tw>
| Date:   Thu Jan 21 10:59:21 2021 +0800
|
|     ready to test conflict #001 on master
|
| * commit ddf5db0cdfb9b27b0c608d11ec98592796da74a9 (testing1)
|/  Author: elite_lin <elite_lin@goglobal.com.tw>
|   Date:   Thu Jan 21 10:51:07 2021 +0800
|
|       ready to test conflict #001
|
* commit 61b294bacfe67576f0df5de8f812a79a63a0ced5
  Author: elite_lin <elite_lin@goglobal.com.tw>
  Date:   Thu Jan 21 08:27:17 2021 +0800

      Commit for the very first time
```

```
D:\Git_Study\my_project_01>git log --oneline --decorate --graph --all
* eca9c5b (HEAD -> master) ready to test conflict #001 on master
| * ddf5db0 (testing1) ready to test conflict #001
|/
* 61b294b Commit for the very first time - 2021/01/21
```

## Merge / Resolve Conflict 


```
git merge <branch-name>
```

因為先前在 ``testing1`` 分支也修改了 ``Program.cs``
所以在 merge 時會遇到 __Merge conflict__,
而 ``LICENSE`` 則是利用 __Auto-merging__ 來化解 Merge conflict

但必須透過 Code review 方式來對修改過的檔案內容進行調整。

```
D:\Git_Study\my_project_01>git merge testing1
Auto-merging Program.cs
CONFLICT (content): Merge conflict in Program.cs
Auto-merging LICENSE
CONFLICT (content): Merge conflict in LICENSE
Automatic merge failed; fix conflicts and then commit the result.
```

## 修改前

* ``Program.cs``:
```
using System;

namespace HelloDotnet
{
    class Program
    {
        static void Main(string[] args)
        {
<<<<<<< HEAD
        	// added comment #001
=======
        	Console.WriteLine("Happy New Year 2021");
>>>>>>> testing1
            Console.WriteLine("Hello Dotnet!");
        }
    }
}

```

* ``LICENSE``:
```
Free to use.

<<<<<<< HEAD
### Hello, I'm writing c# code
=======
I start to study WinForm project development.
>>>>>>> testing1
```

## 修改後

* ``Program.cs``:
```
using System;

namespace HelloDotnet
{
    class Program
    {
        static void Main(string[] args)
        {
        	// added comment #001
        	Console.WriteLine("Happy New Year 2021");
            Console.WriteLine("Hello Dotnet!");
        }
    }
}

```

* ``LICENSE``:
```
Free to use.

### Hello, I'm writing c# code

I start to study WinForm project development.
```

---

在 Code review 並修訂完後，便能重新 add files 並 commit 了

```
D:\Git_Study\my_project_01>git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        both modified:   LICENSE
        both modified:   Program.cs

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        Core/

no changes added to commit (use "git add" and/or "git commit -a")

D:\Git_Study\my_project_01>git add LICENSE

D:\Git_Study\my_project_01>git add Program.cs

D:\Git_Study\my_project_01>git commit -m "Merge from branch `testing1` and resolve conflicts: `Program.cs` and `LICENSE`"
[master c6d8fde] Merge from branch `testing1` and resolve conflicts: `Program.cs` and `LICENSE`

D:\Git_Study\my_project_01>git log --decorate --graph --all
*   commit c6d8fdebf3cd34b6a9bb9629cd2ca2bae20fefc1 (HEAD -> master)
|\  Merge: eca9c5b ddf5db0
| | Author: elite_lin <elite_lin@goglobal.com.tw>
| | Date:   Thu Jan 21 14:22:06 2021 +0800
| |
| |     Merge from branch `testing1` and resolve conflicts: `Program.cs` and `LICENSE`
| |
| * commit ddf5db0cdfb9b27b0c608d11ec98592796da74a9 (testing1)
| | Author: elite_lin <elite_lin@goglobal.com.tw>
| | Date:   Thu Jan 21 10:51:07 2021 +0800
| |
| |     ready to test conflict #001
| |
* | commit eca9c5be565ab1a231ceda316f7469ea1f5b27b3
|/  Author: elite_lin <elite_lin@goglobal.com.tw>
|   Date:   Thu Jan 21 10:59:21 2021 +0800
|
|       ready to test conflict #001 on master
|
* commit 61b294bacfe67576f0df5de8f812a79a63a0ced5
  Author: elite_lin <elite_lin@goglobal.com.tw>
  Date:   Thu Jan 21 08:27:17 2021 +0800

      Commit for the very first time
```


![](https://i.imgur.com/s8LWmSo.png)

---

# [2.5 Git Basics - Working with Remotes](https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes)

## 顯示遠端 (Github, BitBucket, GitLab) Repositories 的狀態

```
git remote
git remote -v
```

``-v`` 會顯示遠端網址

因為我還沒加入遠端的 Repository, 所以使用 ``git remote`` 只看見空的內容!!

## 加入遠端 Repository

此時要先加入遠端的 Repository: 

```
git remote add <shortname> <url>
```

其中, ``shortname`` 可以自己指定, 像我是用 Github, ``shortname`` 就用 ``Github``


為此, 我去 Github 建立一個新的 Repository
(https://github.com/vexonelite/git_cmd_study01)

```
D:\Git_Study\my_project_01>git remote add Github https://github.com/vexonelite/git_cmd_study01.git
```

若是使用 ``git clone`` 把遠端的 Repository clone 下來, 
該指令會自動把遠端 Repository 的網址加入, 並使用預設的 ``shortname`` ``origin``

之後使用 ``git remote -v``:
```
D:\Git_Study\my_project_01>git remote -v
Github  https://github.com/vexonelite/git_cmd_study01.git (fetch)
Github  https://github.com/vexonelite/git_cmd_study01.git (push)
```

## 檢查是否有遠端新的內容

```
git fetch <shortname>
```

這裡的 ``shortname`` 要以 ``git remote`` 顯示的為主，不能自行亂輸入, 否則會看見錯誤:
```
D:\Git_Study\my_project_01>git fetch origin
fatal: 'origin' 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.
```

```
D:\Git_Study\my_project_01>git fetch Github

```

因為沒有更新內容, 所以是空的!


## 將 commit 紀錄 push 到遠端

```
git push <remote> <branch>
```

這裡的 ``remote`` 要以 ``git remote`` 顯示的為主，不能自行亂輸入, 否則會看見錯誤:
```
D:\Git_Study\my_project_01>git push origin master
fatal: 'origin' 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.
```

在加入 repository 時給的 shorthand name 是 ``Github``:
```
D:\Git_Study\my_project_01>git push Github master
Enumerating objects: 16, done.
Counting objects: 100% (16/16), done.
Delta compression using up to 6 threads.
Compressing objects: 100% (14/14), done.
Writing objects: 100% (16/16), 1.47 KiB | 500.00 KiB/s, done.
Total 16 (delta 4), reused 0 (delta 0)
remote: Resolving deltas: 100% (4/4), done.
To https://github.com/vexonelite/git_cmd_study01.git
 * [new branch]      master -> master
```

若專案是2個人以上同時維護, 在同一個 branch 下, 只要有2個人同時在 push, 
遇到 conflict 的情況是可以預期的,
一個基本原則就是, 先將 local 端的變動先進行 commit
然後 fetch 或 pull 遠端的變動,
若有 conflict, 最好是找協同合作的一起看程式來解決 conflicts
然後 commit conflicts , 最後再 push 到遠端!!


為了測試 fetch, 我在還沒進行 push 前, 另外先行進行 clone!

```
D:\Git_Study\my_project_02>git log
fatal: your current branch 'master' does not have any commits yet

```

所以在進行完 push 後, 執行 ``git fetch``:
```
git fetch origin
Enter passphrase for key 'D:\kye_hss\2020_04_05\2020_04_05.pem':
remote: Enumerating objects: 16, done.
remote: Counting objects: 100% (16/16), done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 16 (delta 4), reused 16 (delta 4), pack-reused 0
Unpacking objects: 100% (16/16), done.
From github.com:vexonelite/git_cmd_study01
 * [new branch]      master     -> origin/master
```

``git fetch`` 自動將遠端 repository 的 commit 紀錄下載到本地端的 repository 中, 
但不會進行 merge --> 必須自行 merge:

```
D:\Git_Study\my_project_02>git merge Github/master

D:\Git_Study\my_project_02>git log
commit c6d8fdebf3cd34b6a9bb9629cd2ca2bae20fefc1 (HEAD -> master, Github/master)
Merge: eca9c5b ddf5db0
Author: elite_lin <elite_lin@goglobal.com.tw>
Date:   Thu Jan 21 14:22:06 2021 +0800

    Merge from branch `testing1` and resolve conflicts: `Program.cs` and `LICENSE`

commit eca9c5be565ab1a231ceda316f7469ea1f5b27b3
Author: elite_lin <elite_lin@goglobal.com.tw>
Date:   Thu Jan 21 10:59:21 2021 +0800

    ready to test conflict #001 on master

commit ddf5db0cdfb9b27b0c608d11ec98592796da74a9
Author: elite_lin <elite_lin@goglobal.com.tw>
Date:   Thu Jan 21 10:51:07 2021 +0800

    ready to test conflict #001

commit 61b294bacfe67576f0df5de8f812a79a63a0ced5
Author: elite_lin <elite_lin@goglobal.com.tw>
Date:   Thu Jan 21 08:27:17 2021 +0800

    Commit for the very first time - 2021/01/21

```


## 異動遠端名稱

```git remote rename origin Github```

```
D:\Git_Study\my_project_02>git remote -v
origin  git@github.com:vexonelite/git_cmd_study01.git (fetch)
origin  git@github.com:vexonelite/git_cmd_study01.git (push)

D:\Git_Study\my_project_02>git remote rename origin Github

D:\Git_Study\my_project_02>git remote -v
Github  git@github.com:vexonelite/git_cmd_study01.git (fetch)
Github  git@github.com:vexonelite/git_cmd_study01.git (push)

```


## Git pull

```
git pull <remote> <branch>
```

承上述操作, 
對 ``Program.cs`` 內容進行編輯, 然後 commit 並 push:

```
D:\Git_Study\my_project_02>git status
On branch master
Your branch is up to date with 'Github/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   Program.cs

no changes added to commit (use "git add" and/or "git commit -a")

D:\Git_Study\my_project_02>git add Program.cs

D:\Git_Study\my_project_02>git status
On branch master
Your branch is up to date with 'Github/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   Program.cs

D:\Git_Study\my_project_02>git commit -m "resolve conflict by modifying Program.cs"
[master 0d730fd] resolve conflict by modifying Program.cs
 1 file changed, 3 deletions(-)

D:\Git_Study\my_project_02>git push Github master
Enter passphrase for key 'D:\kye_hss\2020_04_05\2020_04_05.pem':
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 6 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 336 bytes | 336.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:vexonelite/git_cmd_study01.git
   c6d8fde..0d730fd  master -> master

```

然後從另一個 local repository ``my_project_01`` 進行 pull

```
D:\Git_Study\my_project_01>git pull Github master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/vexonelite/git_cmd_study01
 * branch            master     -> FETCH_HEAD
   c6d8fde..0d730fd  master     -> Github/master
Updating c6d8fde..0d730fd
Fast-forward
 Program.cs | 3 ---
 1 file changed, 3 deletions(-)
```


``git pull`` 會執行 ``git fetch`` 
(自動將遠端 repository 的 commit 紀錄下載到本地端的 repository 中), 
然後執行 ``git merge``!!


## 測試多頭 --> conflict --> resolve conflict --> commit & push

分別在兩個本地端的 repositories 
``my_project_01``, ``my_project_02`` 編輯 ``Program.cs``, 然後 commit:

```
D:\Git_Study\my_project_01>git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   Program.cs

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        Core/

no changes added to commit (use "git add" and/or "git commit -a")

D:\Git_Study\my_project_01>git add Program.cs

D:\Git_Study\my_project_01>git commit -m "commit to test conflict by modifying Program.cs (2021/02/03 #1)"
[master 391e227] commit to test conflict by modifying Program.cs (2021/02/03 #1)
 1 file changed, 1 insertion(+)

D:\Git_Study\my_project_01>git log --oneline
391e227 (HEAD -> master) commit to test conflict by modifying Program.cs (2021/02/03 #1)
0d730fd (Github/master) resolve conflict by modifying Program.cs
c6d8fde Merge from branch `testing1` and resolve conflicts: `Program.cs` and `LICENSE`
eca9c5b ready to test conflict #001 on master
ddf5db0 (testing1) ready to test conflict #001
61b294b Commit for the very first time - 2021/01/21
```

```
D:\Git_Study\my_project_02>git status
On branch master
Your branch is up to date with 'Github/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   Program.cs

no changes added to commit (use "git add" and/or "git commit -a")

D:\Git_Study\my_project_02>git add Program.cs

D:\Git_Study\my_project_02>git commit -m "commit to test conflict by modifying Program.cs (2021/02/03 #2)"
[master 0b34e0b] commit to test conflict by modifying Program.cs (2021/02/03 #2)
 1 file changed, 1 insertion(+)
```

然後 ``my_project_02`` 先行 push 到 remote:
```
D:\Git_Study\my_project_02>git push Github master
Enter passphrase for key 'D:\kye_hss\2020_04_05\2020_04_05.pem':
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 6 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 374 bytes | 374.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To github.com:vexonelite/git_cmd_study01.git
   0d730fd..0b34e0b  master -> master

D:\Git_Study\my_project_02>git log --oneline
0b34e0b (HEAD -> master, Github/master) commit to test conflict by modifying Program.cs (2021/02/03 #2)
0d730fd resolve conflict by modifying Program.cs
c6d8fde Merge from branch `testing1` and resolve conflicts: `Program.cs` and `LICENSE`
eca9c5b ready to test conflict #001 on master
ddf5db0 ready to test conflict #001
61b294b Commit for the very first time - 2021/01/21
```

這時從 ``my_project_01`` 要 push 到 remote, 會遇到錯誤:

```
D:\Git_Study\my_project_01>git push Github master
To https://github.com/vexonelite/git_cmd_study01.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/vexonelite/git_cmd_study01.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.
```

此時要進行將遠端的commit 紀錄 fetch 或 pull 回來，進行 merge, 解決 conflit 後，
重新 commit 再 push!!


### Step 1. ``git fetch``:

```
D:\Git_Study\my_project_01>git fetch Github master
From https://github.com/vexonelite/git_cmd_study01
 * branch            master     -> FETCH_HEAD

```

### Step 2. ``git merge``:

```
D:\Git_Study\my_project_01>git merge Github/master
Auto-merging Program.cs
CONFLICT (content): Merge conflict in Program.cs
Automatic merge failed; fix conflicts and then commit the result.
```

### Step 3. 解決 conflit

``Program.cs`` (修改前):

```
using System;

namespace HelloDotnet
{
    class Program
    {
        static void Main(string[] args)
        {
        	// added comment #001
        	Console.WriteLine("2021/01/21");
            Console.WriteLine("Hello Dotnet!");
<<<<<<< HEAD
            Console.WriteLine("2021/02/03_haha");
=======
            Console.WriteLine("2021/02/03_heyhey");
>>>>>>> Github/master
        }
    }
}

```

``Program.cs`` (修改後) :
```
using System;

namespace HelloDotnet
{
    class Program
    {
        static void Main(string[] args)
        {
        	// added comment #001
        	Console.WriteLine("2021/01/21");
            Console.WriteLine("Hello Dotnet!");
            Console.WriteLine("2021/02/03" + "_haha" + "_heyhey");
        }
    }
}
```

### Step 4. commit

```
D:\Git_Study\my_project_01>git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        both modified:   Program.cs

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        Core/

no changes added to commit (use "git add" and/or "git commit -a")

D:\Git_Study\my_project_01>git add Program.cs

D:\Git_Study\my_project_01>git commit -m "resolve conflict and commit [Program.cs] (2021/02/03 #3)"
[master ccc2256] resolve conflict and commit [Program.cs] (2021/02/03 #3)
```

### Step 5. push to remote

```
D:\Git_Study\my_project_01>git push Github master
Enumerating objects: 10, done.
Counting objects: 100% (10/10), done.
Delta compression using up to 6 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 721 bytes | 721.00 KiB/s, done.
Total 6 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 1 local object.
To https://github.com/vexonelite/git_cmd_study01.git
   0b34e0b..ccc2256  master -> master

D:\Git_Study\my_project_01>git log --oneline
ccc2256 (HEAD -> master, Github/master) resolve conflict and commit [Program.cs] (2021/02/03 #3)
0b34e0b commit to test conflict by modifying Program.cs (2021/02/03 #2)
391e227 commit to test conflict by modifying Program.cs (2021/02/03 #1)
0d730fd resolve conflict by modifying Program.cs
c6d8fde Merge from branch `testing1` and resolve conflicts: `Program.cs` and `LICENSE`
eca9c5b ready to test conflict #001 on master
ddf5db0 (testing1) ready to test conflict #001
61b294b Commit for the very first time - 2021/01/21
```


---

# My Questions:

## [What is the origin in Git?](https://www.git-tower.com/learn/git/glossary/origin/#:~:text=In%20Git%2C%20%22origin%22%20is,but%20just%20a%20standard%20convention.)

Git ``origin`` 是遠端 repository 的 __shorthand__ name!!

In Git, ``origin`` is a __shorthand__ name for the remote repository 
that a project was originally cloned from. More precisely, 
it is used instead of that original repository's URL - 
and thereby makes referencing much easier.

Note that ``origin`` is by no means a "magical" name, 
but just a standard convention. 
Although it makes sense to leave this convention untouched, 
you could perfectly rename it without losing any functionality.


In the following example, the URL parameter to the ``clone`` command 
becomes the ``origin`` for the cloned local repository:

```
git clone https://github.com/gittower/git-crash-course.git
```

---

# [How to Rename the master branch to main in Git](https://www.git-tower.com/learn/git/faq/git-rename-master-to-main/)

## Renaming the Local master Branch to main

The first step is to rename the ``master`` branch in your local Git repositories:

```
$ git branch -m master main
```

Let's quickly check if this has worked as expected:

```
$ git status
```
On branch main
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean



So far, so good! The local branch has been renamed - 
but we now need to make some changes on the remote repository as well!

## Renaming the Remote master Branch as Well

In the second step, 
we'll __have to create a new branch on the remote named ``main``__ - 
because Git does not allow to simply "rename" a remote branch. 
Instead, we'll have to create a new ``main`` branch 
and then delete the old "master" branch.

remote branch ``main`` 是要額外建立的,
因為 Git 不允許直接 rename remote branch!!
所以, 要使用 ``main`` branch 取代 ``master`` branch,
只能先建立 ``main`` branch, 然後把 ``master`` branch 給刪除!!

Make sure your current local HEAD branch is still ``main`` 
when executing the following command:

```
$ git push -u origin main
```

We now have a new branch on the remote named "main". Let's go on and remove the old "master" branch on the remote:

```
$ git push origin --delete master
```

Depending on your exact setup, this might have worked and the renaming is successful. In many cases, however, you will see an error message like the following one:

To https://github.com/gittower/git-crashcourse.git
! [remote rejected]   master (refusing to delete the current branch: refs/heads/master)
error: failed to push some refs to 'https://example@github.com/gittower/git-crashcourse.git'
GitHub, like other code-hosting platforms, too, expects you to define a "default" branch - and deleting this is not allowed. Additionally, your old "master" might be set as "protected". You'll need to resolve this before you can go on. Here's how to do this in GitHub:


----


