天天看點

git reset --hard_【Git】--基本使用基本操作Branches分支管理多人協作

本文使用 Zhihu On VSCode 創作并釋出

參考:

  • Github
  • 廖雪峰Git
  • 菜鳥教程

Example: Sync through Github

  1. Init Git:

    git init

  2. Add remote:
    • First, make new repo on github, without makefile save extra work;
    • Then,

      git remote add origin <[email protected]:....>

  3. Push: default remote/branch name: origin/master

    git push <REMOTENAME> <BRANCHNAME>

  4. Fetch: default remote/branch name: origin/master

    git fetch <REMOTENAME> <BRANCHNAME>

clone

  • Clones a repo to your current location, new folder named

    REPOSITORY

    git clone https://github.com/USERNAME/REPOSITORY.git

  • It is init as Git repository (

    git init

    is done)
  • A remote named

    origin

    is created, pointing to the URL you cloned from

fetch

  • Use

    fetch

    to retrieve new work at remote.

    git fetch <REMOTENAME> <BRANCHNAME>

  • Fetching from a repository grabs all the new remote-tracking branches and tags without merging those changes into your own branches.

merge

  • Use

    merge

    to combine local changes with remote

    git merge <REMOTENAME> <BRANCHNAME>

  • Fetching from a repository grabs all the new remote-tracking branches and tags without merging those changes into your own branches.

pull

  • git pull

    is a convenient shortcut for completing both git fetch and git mergein the same command:

    git pull <REMOTENAME> <BRANCHNAME>

基本操作

git有三個區域:

  • Working Tree:工作區
  • Index/Stage:緩存區,add加入的
  • Repository:送出曆史,commit修改的

git add

$ git add .
$ git add [file]
           

加入暫存庫/緩存(Index/Stage)

git status

$ git status -s #short version
>> M Git.md    #red M modified not added, green added
           

git diff

git diff HEAD -- [file]    #檢視working tree和repository差別
           

配合status檢視變化

git commit

$ git commit -m 'comments'
$ git commit -a
$ git commit -am 'comments'   #合并add
           

送出修改到倉庫/分支(Repository)

git log

$ git log --pretty=oneline    #一行顯示一次commit
$ git log --oneline           #一行顯示一次commit, 縮略
$ git log --graph             #圖顯示
           

git reflog

記錄每一次指令

git reset

https://www.jianshu.com/p/c2ec5f06cf1a by carway

#reset --hard:完全放棄工作區,暫存區更改
$ git reset --hard HEAD^
#reset --soft:合并commit
$ git reset --soft HEAD^
#reset mixed:1. unstage 2. 修改commit錯誤
$ git reset HEAD     #1. unstage

$ git reset HEAD^    #2. reset, then delete sth.
$ git commit ...     #submit correct
           

reset實質是移動HEAD,捎帶所指branch

  • --hard:重置位置的同時,直接将 working Tree工作目錄、 index 暫存區及 repository 都重置成目标Reset節點的內容,是以效果看起來等同于清空暫存區和工作區。
  • --soft:重置位置的同時,保留working Tree工作目錄和index暫存區的内容,隻讓repository中的内容和 reset目标節點保持一緻,是以原節點和reset節點之間的 【差異變更集】 會放入 index暫存區中(Staged files) 。是以效果看起來就是工作目錄的内容不變,暫存區原有的内容也不變,隻是原節點和Reset節點之間的所有差異都會放到暫存區中。
  • --mixed:重置位置的同時,隻保留Working Tree工作目錄的內容,但會将 Index暫存區 和 Repository 中的內容更改和reset目标節點一緻,是以原節點和Reset節點之間的 【差異變更集】 會放入Working Tree工作目錄中。是以效果看起來就是原節點和Reset節點之間的所有差異都會放到工作目錄中。

git checkout -- [file]

  • 丢棄工作區working tree的修改,回到add前狀态
  • 注意 -- [file]

git rm

$ git rm [file]            #從stages和working tree同時删除
$ git rm --cached [file]   #從stage删除
$ git rm -r dir            #删除檔案夾

#與rm [file]差別:rm僅删除working tree
           

Branches

git branch

$ git branch [branch_name]    # new branch
$ git branch                  # 檢視目前branch
$ git branch -d [branch_name] # 删除branch
$ git branch -D [branch_name] # 強制删除branch
           

git checkout [branch_name]

$ git checkout -b [branch_name]  # 建立branch 同時跳轉
$ git checkout [branch_name]     # 跳轉
           

switch to that branch, * means which branch on

git switch

$ git switch [branch_name]          #  切換分支
$ git switch -c [branch_name]       #  建立并切換分支
           

分支管理

git merge [branch]

# 直接merge,有時有沖突需解決
$ git merge [branch]
# 為merge操作建立commit
$ git merge --no-ff -m "merge with no-ff" [branch] 
           

分支管理:

  • master用于釋出穩定
  • dev用于開發
  • 個人branch,送出到dev

git stash

$ git stash       #儲存目前現場(使得status clean)
$ git stash list  #檢視保留的現場
$ git stash pop   #恢複現場,同時删除stash
$ git stash apply #恢複現場
$ git stash drop  #删除stash
           

git cherry-pick

$ git cherry-pick [commit] #把bug送出複制到目前分支
           

git rebase [branch]

創造新結點,複制本節點修改,線性拼接到另一[branch]節點(隻連接配接這一節點)

多人協作

clone之後,origin/master與本地master自動對應

本地建立和遠端對應分支

$ git checkout -b branch-name origin/branch-name
           

設定其他branch對應

$ git branch --set-upstream-to=origin/dev dev