git常用命令

2021-10-08 07:20:50 字數 3831 閱讀 2203

參考資料

廖雪峰git教程

本地命令

git helpgit幫助命令

git configgit配置命令

git init初始化乙個repository,或者在乙個已存在的版本庫中重新初始化。

git status顯示狀態

git add ...把檔案加入到索引區index

git add .把當前工作區變動的所有檔案新增到index

git commit -m '提交的資訊'提交index到版本庫

git commit -a -m "commit content"假如嫌git add麻煩,可以直接這樣提交

git commit會強制彈出vim編輯器來編輯『提交資訊』,必須。

git log檢視commit提交歷史

git reflog重返未來,檢視commit歷史

git reset --hard commit_id版本回退。head表示當前版本,reset current head to the specified state

git rm用於刪除乙個檔案,remove files from the working tree and from the index

git rm --cached從版本庫刪除,但是在本地保留。.gitignore

git mv filefrom fileto修改檔案名字或路經,move or rename a file, a directory, or a symlink

git diff檢視工作區和版本庫裡面最新版本的區別

git checkout --可以丟棄工作區的修改,總之,就是讓這個檔案回到最近一次git commit或git add時的狀態。

git reset head把暫存區的修改撤銷掉(unstage),重新放回工作區

git revert是生成乙個新的提交來撤銷某次提交,此次提交之前的commit都會被保留

git reset是回到某次提交,提交及之前的commit都會被保留,但是此次之後的修改都會被退回到暫存區

遠端命令

git clone [email protected]:/.git轉殖遠端版本庫(所有人都可以轉殖github-free)

git remote add origin [email protected]:/.git把本地版本庫的當前分支關聯到遠端伺服器的origin分支(有許可權才可以關聯,sshkey,github-free)

git push -u origin master第一次推送版本到遠端分支

git push origin master推送

git pull origin master從遠端抓取分支,如果有衝突,要先處理衝突

git remote檢視遠端庫的資訊

git remote -v檢視遠端庫的詳細資訊

git checkout -b branch-name origin/branch-name在本地建立和遠端分支對應的分支

git branch --set-upstream branch-name origin/branch-name建立本地分支和遠端分支的關聯

分支git branch檢視版本庫的分支列表

git branch -a加上-a引數可以檢視遠端分支,遠端分支會用紅色表示出來(如果你開了顏色支援的話)

git branch

建立新分支

git branch -d

刪除分支

git checkout

切換到分支

git checkout -b

建立並切換分支

git checkout --track origin/branch_name如果遠端新建了乙個分支,本地沒有該分支。可以利用git checkout --track origin/branch_name,這時本地會新建乙個分支名叫 branch_name ,會自動跟蹤遠端的同名分支 branch_name。

git merge

合併指定分支到當前分支 有2種情況1⃣️fast_forward方式2⃣️conflict方式。第一種方式直接合併,第二種方式先手動解決衝突,再git add,git commit.

標籤git tag建立新標籤 eg:git tag v1.0。預設標籤是打在最新提交的commit上的

git tag檢視所有標籤

git tag v0.9在某次commit上打標籤

git tag -a v1.0 -m "version 1.0 released"建立帶有說明的標籤,用-a指定標籤名,-m指定說明文字

git show檢視標籤資訊

git tag -d可以刪除乙個本地標籤

.gitignore檔案

.gitignore 忽略檔案

git check-ignore -v檢查.gitignore檔案配置檔案規則

git配置git config

git config --list檢視git的配置

git config --global user.name "your name"git config --global user.email "[email protected]"git config --global color.ui true顯示顏色

git config --global alias.st status配置git命令的別名git st代表git status

本地分支關聯遠端倉庫

方法一:(已經建立了本地分支)

git branch --set-upstream-to=origin/remote_branch your_branch

等同於git branch -u origin/remote_branch your_branch方法二:(本地分支不存在,本地名稱與遠端名稱不同)

git branch new_branch origin/remote_branch

// 或者

git fetch origin remote_branch:new_branch

// 上面?這兩種方法都不會切換當前分支;

// 如果想要建立分支的同時,切換到新分支

git checkout -b new_branch origin/remote_branch

常用命令 Git 常用命令大全

安裝教程可參照 廖雪峰老師的安裝教程。git config 在git中,使用git config 命令來配置 git 的配置檔案,git配置級別主要有3類 1 倉庫級別 local 本地 git 倉庫級別配置檔案,作用於當前倉庫。優先順序最高 2 使用者級別 global,全域性配置檔案,作用於所有...

git 常用命令

檢視是否存在檔案需要上傳 git status git add git commit m 建立遠端倉庫 git remote add origin 116.255.146.153 ruby cd work daily project.git 更新git fetch 116.255.146.153 r...

git常用命令

詳細 1,git log p 命令來顯示每一次提交與其父節點提交內容之間快照的差異。2,為了檢視載入 staged 而並未提交 not committed 的內容差異,可以使用 git diff stage 命令 在git 1.6之前的版本中,使用 cached 適應情形 在執行git commit...