记录一些常用的 Git 命令,便于查看
config
设置用户名和邮箱
git config –global user.name "[name]"
git config –global user.email "[email]"
别名配置
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
init
初始化一个仓库
# 将当前目录初始化成一个Git代码库
git init
# 新建一个目录,将其初始化为Git代码库
git init [repository name]
clone
# 克隆现有的仓库
git clone [repository url]
# 克隆现有的仓库并自定义本地仓库名
git clone [repository url] [new repository name]
add
暂存改变的文件
# 暂存指定文件
git add [file1] [file2] ...
# 暂存所有改变(新增,修改,删除)
git add -A
# 暂存所有改变(新增,修改,删除),git v1 版本不会暂存 删除文件
git add .
# 暂存除删除外的所有改变(新增,修改)
git add --ignore-removal .
# 暂存除新增外的所有改变(修改,删除)
git add -u
commit
提交
git commit -m "[commit message]"
# 跳过 add 步骤,直接提交
git commit -a -m "[commit message]"
fetch
# 下载远程仓库的所有变动
git fetch [remote]
pull
# 取回远程仓库的变化,并与本地分支合并
git pull [remote] [branch]
push
# 上传本地分支 branch1 到远程仓库
git push origin branch1
# 强行推送当前分支到远程仓库,即使有冲突
git push [remote] --force
# 推送所有分支到远程仓库
git push [remote] --all
checkout
# 切换分支
git checkout branch1
# 创建并切换到 branch1 分支
git checkout -b branch1
# 丢弃指定文件的修改,恢复至上一次 git commit 或 git add 时的状态
git checkout -- file
branch
# 查看分支
git branch
# 创建分支 branch1
git branch branch1
# 删除分支
git branch -d branch1
merge
# 合并 branch1 到当前分支
git merge branch1
diff
# 比较当前分支文件的修改
git diff
# 比较指定文件的修改
git diff file
status
查看当前分支的状态
git status
log
查看提交记录
git log
git log --pretty=oneline
reflog
查看操作的历史命令记录
git reflog
reset
# HEAD^ 表示上一个版本,HEAD^^ 表示上上一个版本,HEAD~n 表示回退 n 个版本
git reset --hard HEAD^
# 回退到指定提交,commit id 没必要写全,前几位就可以了,Git会自动去找
git reset --hard [commit id]
关联并提交到远程仓库
git remote add origin [repository url]
git push -u origin master