Git經常使用命令

建立倉庫 Create respository

建立倉庫

$ git init

SSH密鑰

ssh密鑰可以受權遠程Git服務器識別你的計算機,只有在遠程服務器上登記了SSH密鑰才能向其提交代碼。git

# Generate the SSH key
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

# Verify the availability of SSH  
$ ssh -T <remote index>

# example
$ ssh –T git@github.com

快照管理

添加修改

添加(add)可以把改動保存在暫存區(stage)。github

$ git add <file>
$ git add -A    # add all files in this respository
$ git add .    # same as command "git add -A"

查看狀態

查看添加修改後的文件改動概況。web

$ git status

查看文件改動

查看各文件的具體改動,包括行的添加,修改和刪除。vim

$ git diff

提交修改

$ git commit
$ git commit -m "<commit comment>"

返回過往提交

$ git reset <commit id>

查看記錄

$ git log
$ git log --graph    # display log with graph

分支管理

分支(branch)區別於主幹(master),它是平行於主幹的,在上面作出任何修改都不會干擾到主幹的內容,直到你把分支與主幹合併,分支和主幹的內容也隨之合併,至關於應用了分支的修改。適用於分佈式異步的開發。bash

建立分支

$ git branch <branch name>

查看分支

# Check the all of local branch.
$ git branch

# Check the all of remote and local branch.
$ git branch -a
*master
 dev
 debug
# the branch which its name has front asterisk signal * 
# is current branch.

切換分支

$ git checkout <branch name>

創建遠程倉庫分支

# create new local branch relate to remote branch
$ git branch <local_branch> <remote>/<remote_branch>    

# exist local branch relate to remote branch
$ git branch --set-upstream <local_branch> <remote>/<remote_branch>

遠程倉庫

遠程倉庫(remote repository)是區別於本地倉庫(local repository)的基於Git的代碼倉庫,它能夠是開發小組中統一管理項目的主機,也能夠是相似Github這樣的在線代碼倉庫。服務器

克隆倉庫

$ git clone <remote>

查看遠程倉庫

$ git remote

# display verbose remote address with more detail.
$ git remote -v

增長遠程倉庫

$ git remote add <remote alias> <remote url>

修改遠程倉庫

$ git remote rm <remote alias>    # remove remote  
$ git remote rename <old name> <new name>    # rename remote

從遠程倉庫拉取

常常用於倉庫的更新,特別是軟件的更新。ssh

$ git pull

向遠程倉庫推送

$ git push <remote> <branch>

標籤管理

標籤(tag)用於管理每次提交(commit),由於每次提交都具備一個提交代號(commit id),識別起來不方便,因此能夠用標籤來表明特定的提交,使每次提交更有可讀性和便於管理,通常是用於標識版本號。異步

增長標籤

# add a new tag defaultly to latest commit in current branch
$ git tag <tag_name>

# add a new tag to specified commit
$ git tag <tag_name> <commit_id>  

# add a tag with message
$ git tag -a <tag_name> -m "<tag_message>" <commit_id>

查看標籤

$ git tag  # show all tags
$ git show <tag_name>  # show specific tag detail

刪除標籤

$ git tag -d <tag_name>  # delete a specific local tag
$ git push <remote> :refs/tags/<tag_name>  # delete remote specific tag

推送標籤

$ git push <remote> <tag_name>  # push specific tag to remote
$ git push <remote> --tags  # push all tags to remote

Git設置

config命令

# format
$ git config <option> <varriable> <value>

# example
$ git config --global user.name "your_name"
$ git config --global user.email "yourname@website.com"
$ git config --global color.ui true

修改別名

別名,能夠自定義命令,常常用於縮略冗長的命令,達到快速輸入的效果。在用戶目錄的隱藏文件".gitconfig"中能夠修改別名分佈式

$ cd ~
$ find ".gitconfig"
$ vim .gitconfig

在「[alias]下」,若是沒有,則本身定義,輸入自定義的命令別名網站

[alias]
	mylog = log --graph

參考資料

Git教程 - 廖雪峯的官方網站

Git - Documentation

相關文章
相關標籤/搜索