1. 程式人生 > 實用技巧 >Git常用命令以及場景問題解決

Git常用命令以及場景問題解決

Git常用命令以及場景問題解決

指令

新建程式碼庫

# 在當前目錄新建一個Git程式碼庫
git init

# 新建一個目錄,將其初始化為Git程式碼庫
git init [project-name]

配置

# 顯示當前的Git配置
git config --list

# 設定提交程式碼時的使用者資訊
git config [--global] user.name "name"
git config [--global] user.email "email address"

新增檔案

# 新增指定檔案到暫存區
git add [file1] [file2] ...

# 新增指定目錄到暫存區,包括子目錄
git add [dir]

# 添加當前目錄的所有檔案到暫存區
git add .

提交

# 提交暫存區到倉庫區
git commit -m "msg"

# 提交暫存區的指定檔案到倉庫區
git commit [file1] [file2] ... -m "msg"

# 提交工作區自上次commit之後的變化,直接到倉庫區
git commit -a

# 提交時顯示所有diff資訊
git commit -v

分支

# 列出所有本地分支
git branch

# 列出所有遠端分支
git branch -r

# 列出所有本地分支和遠端分支
git branch -a

# 新建一個分支,但依然停留在當前分支
git branch [branch-name]

# 新建一個分支,並切換到該分支
git checkout -b [branch]

# 切換到指定分支,並更新工作區
git checkout [branch-name]

# 切換到上一個分支
git checkout -

# 合併指定分支到當前分支
git merge [branch]

# 刪除分支
git branch -d [branch-name]

# 刪除遠端分支
git push origin --delete [branch-name]

git branch -dr [remote/branch]

標籤

# 列出所有tag
git tag

# 新建一個tag在當前commit
git tag [tag]

# 新建一個tag在指定commit
git tag [tag] [commit]

# 刪除本地tag
git tag -d [tag]

# 刪除遠端tag
git push origin :refs/tags/[tagName]

# 檢視tag資訊
git show [tag]

# 提交指定tag
git push [remote] [tag]

# 提交所有tag
git push [remote] --tags

# 新建一個分支,指向某個tag
git checkout -b [branch] [tag]

其他

# 普通克隆
git clone [url]

# 帶分支克隆
git clone -b [url]

# 相當於進行了 git fetch 和 git merge兩部操作
git pull origin master 

# 強行推送當前分支到遠端倉庫,即使有衝突<慎用>
git push [remote] --force

# 上傳本地指定分支到遠端倉庫
git push [remote] [branch]

# 顯示所有遠端倉庫
git remote -v

常見錯誤

You have not concluded your merge (MERGE_HEAD exists)

可能是因為以前pull下來的程式碼自動合併失敗導致的。

  • 保留本地的更改
# 1. 中止合併
git merge --abort
# 2. 重新合併
git reset --merge
# 3. 重新拉取<如果有衝突,再解決衝突>
git pull

.gitignore

HELP.md
target/
log/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**
!**/src/test/**

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/

### VS Code ###
.vscode/