# Git

pipeline

```bash
git init

```

```bash
git stash
git pull
```

### Check Last Commit Status

Show last one commit

{% code title="Git Command" %}

```bash
git log -1 --stat
```

{% endcode %}

{% code title="Output" %}

```bash
C:\WorkingDirectory> git log -1 --stat
commit 06296cef386d8bf06bdc70e0845b229584cacaca (HEAD -> master)
Author: Ryan <ryan.lam@amox.hk>
Date:   Fri Oct 5 00:13:17 2018 +0800

    update

 XXX/xxx.py | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

```

{% endcode %}

Show last two commits

```bash
git log -2 --stat
```

## Add Remote repo

#### In Terminal, add the URL for the remote repository where your local repository will be pushed.

### set the new remote

```bash
git remote add gitlab https://gitlab.com/asiabots/asset-team/nlp/datetime-extractor/datetime-extractor.git
```

"gitlab" << remote repo name

### Verifies the new remote URL

```bash
git remote -v
```

### Push the changes in your local repository to GitHub

```bash
git push -u gitlab master
```

## 更新從 GitHub 上 fork 出來的 repository (或是同步兩個不同 server 端的 repository)

發佈日期: [2014-02-06](https://www.peterdavehello.org/2014/02/update_forked_repository/)

從 [GitHub](http://https/github.com) 上面 fork 出來的 repository，整個狀態會停留在當初 fork 的時候，後面的同步要靠自己動手，當然你也可以把 fork 出來的 repository 整個砍掉重新 fork，這邊要講的是手動同步、而不用砍掉重練的方法，有兩個不同的 [git](http://https/git-scm.com/) server 要做同步的動作也是這樣做，有些專案會同時丟在自己的 git server 跟 GitHub 上，就會需要用到

大致講一下流程 … 操作必須是在自己 local 端做 (希望以後 GitHub 可以推出按一個鈕就幫我去同步到最新XD)，做法是把 fork 出來的 repository 在本地更新後、再把 repository 給 push 出去！會需要用到 git 的 branch, remote, pull, push 觀念。

接下來的操作環境是你已經把你 fork 出來的 repository 給 clone 到自己的電腦上了， clone 好之後請切換到對應的資料夾底下，然後就可以開始使用 git 做操作了。

第一次操作時我們要加入一個遠端的 remote 當作更新來源，如果要比較是否有加入成功的話可以在操作前後先看狀態：\
`$ git remote -v`

預設應該只會有 origin 這個 remote：\
`origin` [`https`](http://https/en.wikipedia.org/wiki/HTTPS)`://github.com/user/repo.git (fetch)`\
`origin https://github.com/user/repo.git (push)`

我們用下面這個命令來加入遠端的 repository，在這邊的情境也就是比較新的、上游的 repository\
upstream 是 remote name、可以自己取名，不要重複就好，但後面我都用會 upstream 做示範\
而後面那串網址是 repository 位置：\
`$ git remote add upstream https://github.com/otheruser/repo.git`

如果再看一次現有的remote端應該會發現多了兩組 upstream (fetch & push)：\
`$ git remote -v`\
`origin https://github.com/user/repo.git (fetch)`\
`origin https://github.com/user/repo.git (push)`\
`upstream https://github.com/otheruser/repo.git (fetch)`\
`upstream https://github.com/otheruser/repo.git (push)`<br>

有了遠端的來源後我們就可以開始做更新了

假如我要做更新的的 branch 是 master 的話就先切到 master branch：\
`$ git checkout master`

接著把 upstream 的 master 更新給拉進來\
`$ git pull upstream master`

如果你的 master branch 有自己的 commit, 也可以用 rebase 來避免不必要的 merge 操作:\
`$ git pull --rebase upstream master`

如果沒有發生衝突的話這樣應該就完成了本地的更新，再把更新後的 branch push 出去就行了\
`$ git push origin master`

這時再回去看看你的 repository 頁面，應該會發現已經同步到最新的狀態了！

reference:\
<https://help.github.com/articles/syncing-a-fork>

<https://www.peterdavehello.org/2014/02/update_forked_repository/>

## overwrite a branch on another branch

```bash
$ git checkout new_branch
$ git merge -m 'merge message' -s ours origin/old_branch
$ git checkout old_branch
$ git merge new_branch
$ git push origin old_branch
```

<https://stackoverflow.com/questions/4624357/how-do-i-overwrite-rather-than-merge-a-branch-on-another-branch-in-git>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://asiabots.gitbook.io/learning/git.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
