git基礎(chǔ) (1):git基本用法

一、創(chuàng)建git倉庫

# 將當(dāng)前目錄初始化為git倉庫,等價于:git init .
$ git init

$ git init repo-dir  # 將指定的目錄初始化為git倉庫 (如果指定的目錄不存在,則創(chuàng)建)


二、創(chuàng)建分支、添加內(nèi)容、提交內(nèi)容

$ git branch -l # 列出所有存在的分支 (本地)
$ git branch -a # 列出所有分支 (本地和遠(yuǎn)程)

# 剛創(chuàng)建的倉庫沒有任何分支,第一次執(zhí)行 "git add something" 后,會生成一個分支master分支,但是并未正則創(chuàng)建,第一次commit提交后會真正的創(chuàng)建master分支。
# 創(chuàng)建個文件并輸入一點(diǎn)內(nèi)容 "vim hello.c" 輸入一點(diǎn)內(nèi)容
$ git add hello.c   # 向當(dāng)前分支添加文件
$ git add . # 添加當(dāng)前目錄下的所有文件 (包括當(dāng)前目錄下的子目錄中的文件)
$ git status # 查看當(dāng)前狀態(tài) (會有 On branch master,此時用 git branch -l查看,還是沒有任何分支)
$ git commit -m "添加第一個文件" # 此時已經(jīng)生成了第一個分支:master (此時執(zhí)行 "git branch -l" 可以看到master分支了)

$ git branch   # 查看當(dāng)前位于哪個分支上

$ git branch new-branch-name # 創(chuàng)建一個分支,如果new-branch-name已經(jīng)存在,則報錯。
# tips:git branch branch-name,
# 1. 是基于當(dāng)前分支創(chuàng)建新的分支,branch_name分支 與 當(dāng)前所在分支的內(nèi)容一樣。
# 2. 只是創(chuàng)建一個新分支,并不會切換。

$ git branch -d test # 刪除本地分支 test。

# 切換分支
$ git checkout branch-name # 切換分支
$ git checkout -b new-branch-name # 創(chuàng)建并切換分支

$ git log   # 查看當(dāng)前分支的提交記錄


三、將本地git倉庫,上傳到 gitee 或 github

在github上創(chuàng)建一個倉庫,并配置SSH key。(如何在github上創(chuàng)建倉庫,以及如何配置SSH key,請自行百度)

# 將本地倉庫與github上的倉庫關(guān)聯(lián)起來
# git remote add origin https://github.com/phoenix19900219/jni-demo.git
$ git remote add origin https://gitee.com/stone100/jni-demo.git

############################################
############################################
# 由于github抽風(fēng), 下面這段不是正常流程 (后面改用gitee)
$ git pull   # pull前需要把當(dāng)前分支與遠(yuǎn)程分支關(guān)聯(lián)起來,另外要使用--rebase來合并本地與遠(yuǎn)程的內(nèi)容,避免遠(yuǎn)程分支的內(nèi)容把本地內(nèi)容給覆蓋了
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (5/5), 4.70 KiB | 687.00 KiB/s, done.
From https://github.com/phoenix19900219/jni-demo
 * [new branch]      main       -> origin/main
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> master


# github上創(chuàng)建git倉庫時,創(chuàng)建的默認(rèn)分支是main
$ git branch -a  # 發(fā)現(xiàn)了一個遠(yuǎn)程分支:remotes/origin/main
* master
  remotes/origin/main

# 創(chuàng)建一個main分支并切換到main分支上
$ git checkout -b main

# 將當(dāng)前分支與遠(yuǎn)程分支關(guān)聯(lián)起來
$ git branch --set-upstream-to=origin/main

$ git pull --rebase 

$ git remote  # 查看關(guān)聯(lián)的遠(yuǎn)程倉庫有哪些
$ git remote remove origin # 刪除遠(yuǎn)程倉庫
############################################
############################################

# 拉取遠(yuǎn)程倉庫origin上的master分支,并與當(dāng)前分支合并
$ git pull --rebase origin master


# git push 命令的格式如下:
# git push <遠(yuǎn)程主機(jī)名> <本地分支名>:<遠(yuǎn)程分支名>

# git push # 如果只有一個遠(yuǎn)程主機(jī)(遠(yuǎn)程倉庫),那么遠(yuǎn)程倉庫可省略,默認(rèn)的本地分支為當(dāng)前分支,默認(rèn)的遠(yuǎn)程分支為本地分支所關(guān)聯(lián)的那個分支。


# 本地分支如何關(guān)聯(lián)遠(yuǎn)程分支??
$ git branch -a   # 列出本地和遠(yuǎn)程分支,輸出如下:
* master
  remotes/origin/master

$ git branch -u remotes/origin/master # 將當(dāng)前分支與遠(yuǎn)程分支remotes/origin/master關(guān)聯(lián)起來

# git branch 的參數(shù):
#    -u, --set-upstream-to <upstream>
#                          change the upstream info
#    --unset-upstream      unset the upstream info
# 說明:
# -u, --set-upstream-to  設(shè)置當(dāng)前分支關(guān)聯(lián)的遠(yuǎn)程分支
# --unset-upstream       取消當(dāng)前分支關(guān)聯(lián)的遠(yuǎn)程分支

# remote倉庫如果是多個則需要指明
# git push origin             # 指明remote倉庫為origin
# git push origin master      # 指明遠(yuǎn)程倉庫和遠(yuǎn)程分支
$ git push origin master:remotes/origin/master # 指明遠(yuǎn)程倉庫, 本地分支, 遠(yuǎn)程分支


四、從github上clone一個項目到本地

# git clone 遠(yuǎn)程倉庫名
# 如:
$ git clone https://gitee.com/stone100/jni-demo.git


五、把遠(yuǎn)程倉庫內(nèi)容更新到本地

# 1. 關(guān)聯(lián)遠(yuǎn)程倉庫
$ git remote add orgin <remote-repo-url>

# 2. 關(guān)聯(lián)某個遠(yuǎn)程倉庫的某個分支
$ git branch -u remote-branch-name # 可以使用 "git branch -a" 查看遠(yuǎn)程分支的名稱

# 3. 拉取并合并
$ git branch --rebase origin master
# 當(dāng)然,你也可以使用 git fetch + git merge 的方式,來實現(xiàn)更新同步。

# 關(guān)于:rebase 和 merge 的區(qū)別:
# https://www.cnblogs.com/kevingrace/p/5896706.html


六、總結(jié)

在本地建立一個git倉庫并推送到gitee或github上的流程:

# 1. 初始化git倉庫
$ git init git-demo

# 2. 添加一點(diǎn)東西 并 提交
$ git add hello.cpp
$ git commit -m "第一提交"

# 3. 在gitee 或 github 上創(chuàng)建一個repository

# 4. 將本地倉庫與gitee或github上的倉庫關(guān)聯(lián)起來
$ git remote add origin https://gitee.com/stone100/jni-demo.git

# 5. 把當(dāng)前分支與遠(yuǎn)程倉庫的分支關(guān)聯(lián)起來
$ git branch -u remotes/origin/master # 使用 "git branch -a" 來查看遠(yuǎn)程分支的名稱

# 6. 拉取遠(yuǎn)程分支內(nèi)合并到本地分支
$ git pull --rebase origin master

# 7. 將本地內(nèi)容推送到遠(yuǎn)程倉庫
$ git push origin master:remotes/origin/master


七、更多高級用法:

https://gitee.com/stone100/git-demo

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 前言 每次換電腦的時候老是遇到一些git的“bug”,所以這次決定整理一下git的基礎(chǔ)的用法,希望也能對小伙伴們有...
    墨塵_7閱讀 287評論 0 1
  • 主要是基于廖雪峰官網(wǎng)git教程的學(xué)習(xí)筆記;感謝大佬們的分享,努力做一個海邊拾貝的boy~ 0、背景 Git是目前世...
    小貝學(xué)生信閱讀 171評論 0 0
  • Git是目前世界上最先進(jìn)的分布式版本控制系統(tǒng),也是當(dāng)下最流行的版本控制,不管是IT行業(yè),還是小說家等等,使用了Gi...
    程序猿在廣東閱讀 1,331評論 2 14
  • git安裝好后的配置用戶信息命令git config --global user.name "賬戶名"git co...
    guyigg閱讀 1,085評論 0 1
  • 查看用戶名和郵箱地址: $ git config user.name $ git config user.emai...
    篆刻bug閱讀 247評論 0 1

友情鏈接更多精彩內(nèi)容