Git 常用命令
常见参数:
-f
、--force
:强制-d
、--delete
:删除-D
:--delete --force
的快捷键-m
、--move
:移动或重命名-M
:--move --force
的快捷键-r
、--remote
:远程仓库-a
、--all
:所有
分支操作
查看本地所有分支:
git branch
;查看远程所有分支:
git branch -r
;查看本地和远程所有分支:
git branch -a
;新建分支:
git checkout -b {branch_name}
-b
参数表示是否切换至新分支
删除分支:
git branch -d -r {branch_name}
-r
参数表示删除本地分支还是远程分支
删除远程分支:
git branch -d -r origin/{branch_name}
git push origin :{branch_name}
重命名分支:
git branch -m {old_branch_name} {new_branch_name}
重命名远程分支:
删除远程老分支:
git branch -d -r {old_branch_name}
;push
本地新分支到远程:git push origin {new_branch_name}:{new_branch_name}
;
检出某个
comment
作为新分支:git checkout {comment_id} -b {branch_name}
小贴士
查询用户名和邮箱:
$ git config user.name
$ git config user.email
编辑用户名和邮箱:
$ git config --global user.name "{name}"
$ git config --global user.email "{email}"
取消换行符自动转换:
git config --global core.autocrlf false
;修改最后一次注释:
git commit --amend
;已经在版本库里的文件无法直接通过
ignore
排除,需要先执行:git rm --cached {file_path}
git update-index --assume-unchanged {file_path}
修改远程仓库地址:
使用修改命令:
git remote set-url origin {url}
先删除后新增:
* `git remote rm origin` * `git remote add origin {url}`
修改
config
文件
生成
.git-credential
记录用户名密码,之后操作便不用输入账号密码:git config --global credential.helper store
;丢弃本地所有没有
commit
的修改:git checkout .
;把所有没有提交的修改暂存:
git stash
,可用git stash pop
恢复;返回到某个版本,不保留修改:
git reset --hard {commit_id}
;返回到某个版本,保留修改:
git reset --soft {commit_id}
;查看提交日志:
git log
;