首页 > 技术文章 > git常用命令大全

zhou-920644981 2019-07-08 17:21 原文

初始化目录变成git管理的仓库:git init

查看仓库当前状态:git status

查看差异:git diff

增加修改到暂存区:git add <filename>

增加所有修改到暂存区:git add .

提交到本地分支:git commit -m "comment..."

查看提交日志:git log

查看提交日志(一行输出):git log --pretty=oneline

回退版本:git reset --hard <commit_id>

回退到上一个版本:git reset --hard HEAD^

撤销修改:git checkout -- <filename>

删除文件:git rm <filename>

查看分支:git branch

创建分支:git branch <branch_name>

切换分支:git checkout <branch_name>

创建+切换分支:git checkout -b <branch_name>

合并某分支到当前分支:git merge <branch_name>

删除分支:git branch -d <branch_name>

删除远程分支:git push origin --delete <branch_name>

推送本地分支到远程分支:git push origin <local_branch_name>:<origin_branch_name>

强行删除没有被合并的分支:git branch -D <branch_name>

查看分支合并情况:git log --graph --pretty=oneline --abbrev-commit

将当前修改搁置:git stash

查看之前搁置的修改:git stash list

恢复之前搁置的修改:git stash apply

删除之前搁置的修改:git stash drop

恢复并删除之前搁置的修改:git stash pop

查看远程分支信息:git branch -r

查看远程库信息:git remote -v

获取远程分支到本地:git checkout -b dev origin/dev

创建本地分支和远程分支的联系:git branch --set-upstream branch_name origin/branch_name

更新本地分支:git pull

推送修改到远程分支:git push origin branch_name

新建一个标签(默认为HEAD):git tag <tag_name>

指定commit_id新建一个标签:git tag <tag_name> <commit_id>

指定标签信息:git tag -a <tag_name> -m "comment..."

指定所有标签:git tag

删除标签:git tag -d <tag_name>

删除远程标签(要先删除本地标签):git push origin :refs/tags/<tag_name>

推送标签到远程:git push origin <tag_name>

推送所有标签到远程:git push origin --tags

查看已经合并的分支:git branch --merged

查看没有合并的分支:git branch --no-merged

删除缓存的远程分支列表:git remote prune origin

 

延伸:可以配置别名,提高效率
$ git config --global alias.co checkout
$ git config --global alias.cm commit
$ git config --global alias.br branch
$ git config --global alias.st status

 


全局配置

$ git config --global user.name "Lucyboy"
$ git config --global user.email "luckboy@hear.com"

 


创建仓库的两种方式

1. 从git server上clone项目
$ git clone git@gitserver:interfaces/interfaces.git
$ cd interfaces
$ touch README.md
$ git add README.md
$ git commit -m "add README"
$ git push -u origin master
2. 将已有项目提交到git server上
$ cd existing_folder
$ git init
$ git remote add origin git@svnserver:interfaces/interfaces.git
$ git add .
$ git commit
$ git push -u origin master

推荐阅读