Git 命令梳理

只梳理在终端常输入的命令,复杂命令一般用 SourceTree 操作。

本文中的 remote 一般默认就是 origin,可相互替代。

创建仓库

#本地初始化一个仓库
git init
#克隆远程仓库 并重命名仓库名(可选)
git clone [url] [dir_name]
#克隆远程仓库 指定分支
git clone -b <branch> <url> <dir_name>

分支命令

#创建分支
git branch branchName

#切换分支
git checkout branchName

#创建并切换分支
git checkout -b branchName

#创建追踪远程的分支
#追踪分支更新执行 git pull ,git push 即可
git checkout -t|[--track] <remote>/<tracked branch>  ##创建的分支名跟远程分支相同
git checkout --track -b <local branch> <remote>/<tracked branch>
git checkout -b branchName <remote>/<tracked branch>

eg: git checkout  --track -b dev_20170331 origin/dev_20170331

#跟踪远程分支 -u 或 --set-upstream-to 
git branch -u <remote>/<branchName>
git push -u <remote>/<branchName>

# 从指定的  创建分支
git checkout tagName -b branchName

#合并分支
git merge branchName

#拉取远程分支
git pull remote branchName

#推送到远程分支
git push remote branchName

#更新
git fetch

#删除本地分支
git branch -d|[-D] branchName

#删除远程分支
git push <remote> --delete branchName

提交

#to stage  to include/update what will be committed
git add [file]|[dir]|[.]

#to unstage
git rm --cached [-r] <file>

#提交更新
git commit -m msg
#add 并 commit
git commit -a -m msg
git commit -am msg

#替换上次提交记录 --amend
git commit -m msg --amend

#恢复到工作区 discard changes in working directory
git checkout -- <file>

#恢复 HEAD
git reset --hard HEAD

标签

#删除本地标签
git tag -d tagName

#删除远程标签
git push origin :refs/tags/tagName

配置

#设置大小写敏感
git config --global core.ignorecase false

#设置显示中文文件名
git config --global core.quotepath false 

#查看配置
git config --list

别名配置

可以简化命令

可以打开 gitconfig 配置,然后在 alias 处修改 (推荐)。

open ~/.gitconfig 

lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative

co = checkout
br = branch
ci = commit
st = status
#查看贡献
rank = shortlog -sn --no-merges 

cob = checkout -b

也可以敲命令配置,作用相同。


git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status

常用操作

移除远程的.idea文件夹

有时候一不小心把 .idea 文件夹 push 到了远程,这时候再加 gitignore 已经没用了(已经跟踪了的文件,再无视,是没有效果的)。

  1. 执行 git rm -fr .idea
  2. 重新加载项目
  3. 在 .gitignore 里添加 /.idea 忽视 idea
  4. 再 gcam “rm idea” git push 到远程

这个方法也适用于其他的文件。

资料

ProGit

    原文作者:程序亦非猿
    原文地址: https://www.jianshu.com/p/2f26b7b98298
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞