Git常用命令


title: Git常用命令
comments: false
date: 2019-08-16 20:07:32
description: 总结日常工作中常用的Git命令, 记录下来。
categories: Git


概述

记录一下工作中常用的 Git 命令。

常用命令

使用前配置

# 配置用户名和邮箱,以便在提交代码时知道你是谁
git config --global user.name="xxx"
git config --global user.email="xxx@126.com"
# 配置Git默认编辑器
git config --global core.editer="vim"
# 配置换行符自动转换
# true: 提交时转换为LF, 检出时转换为CRLF
# input: 提交时转换为LF, 检出时不转换
# false: 提交检出均不转换
git config --global core.autoctlf false
# false: 不做任何检查
# warn: 在提交时检查并警告
# true: 在提交时,如果发现混用则拒绝提交
git config --global core.safectlf true

提交代码

# 检查本地仓库状态
git status
# 添加到工作区, 使 Git 可以追踪文件变化, 可以直接添加目录, 比如当前目录(.)
git add .
# 提交到暂存区
git commit -m '{message}'
# 提交到远程仓库
git push
# 从 Git 中删除文件, 与 rm 命令相似
git rm xxx
# 重命名,与 mv 命名相似
git mv xxx newXXX

分支管理

# 查看所有分支(本地 + 远程)
git branch -a
# 创建分支
git branch {branch_name}
# 切换分支
git checkout {branch_name}
# 提交分支到远程仓库, 并设置追踪
git push -u origin {branch_name}
# 合并分支
git merge {branch_name}
# 删除本地分支
git branch -d {branch_name}
# 删除远程分支
git push -u origin --delete {branch_name}
# 出现冲突时使用 git add xxx 告诉 Git 冲突已经解决了

查看历史

# 基本命令
git log
# 简介版本的 log
git log --oneline
# 开启拓扑图
git log --oneline --graph
# 查看某某某提交的记录, 限制为 5 行
git log --author=xxx --oneline -5

关联远程仓库

# 初始化当前目录为 Git 项目
git init
# 添加当前项目的所有文件到工作区
git add .
# 提交到暂存区
git commit -m "first commit"
# 添加一个远程仓库
git remote add origin git@github.com:snailwudada/xxxx.git
# 推送到远程仓库的 master 分支
git push -u origin master

Mac 用户或 Linux 用户可以装一个 oh-my-zsh, 它会自动给你 alias 很多的 git 命名,很实用。

原文地址:https://www.cnblogs.com/wuqinglong/p/11366104.html