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/

原文地址:https://www.cnblogs.com/jockming/p/13863824.html