git常用命令总结这里写代码片
git配置(config):
git version # 查看版本
git config -l # 查看当前配置
git config --global user.name "Dean" # 设置用户名,邮箱
git config --global user.email [email protected] # 设置用户名,邮箱
git config --global alias.ci commit # 设置git命令的别名
git config --global alias.co checkout # 设置git命令的别名
git仓库(repository):
# 创建一个本地的git仓库并命名:
git init demo
# 克隆一个远程的git仓库到指定路径:
git clone https:
git分支(branch):
git branch
git remote show origin
git branch <branchname>
git checkout <branchname>
git checkout -b <new_branch>
git branch -d <branchname>
git branch -m <old> <new>
git添加(add):
git add <file>
git add .
git add -u
git add -A
git add -i
git删除/重命名(rm/mv):
git rm <file> # 删除文件
git rm -r <floder> # 删除文件夹
git rm --cached <file> # 从版本库中删除文件,但不删除文件
git mv <old_name> <new_name> # 文件重命名
git提交(commit):
git commit -m "comment" # 提交暂存区中的内容(已经add)并添加注释
git commit -a # 把修改的文件添加到暂存区(不包括新建(untracked)的文件),然后提交。
git commit --amend # 修改提交的commit(没有push)
git commit --amend -m "comment" # 修改commit注解
git差异(diff)
git diff
git diff --cached
git diff --staged
git diff HEAD
git diff --stat
git diff commit1 commit2
git查看历史(log):
git log
git log -3
git log --oneline
git log -p
git log --stat
git log --graph
git查看状态(status):
git status
git status -s
git status --ignored
git存储(stash):
git stash
git stash save "message"
git stash list
git stash pop
git stash apply
git stash drop
git stash clear
git重置(reset):
git reset --mixed
git reset --soft
git reset --hard
git reset
git reset HEAD
git reset filename
git reset HEAD^
git reset --soft HEAD~3
git撤销(revert):
git revert commit # 撤销指定commit
git revert HEAD # 撤销上一次commit
git revert -no-edit HEAD # 撤销上一次并直接使用默认注释
git revert -n HEAD # 撤销上一次但不commit
git遴选(cherry-pick):
git cherry-pick <commit_id>
git合并(merge):
git merge <branch_name> # 合并
git merge --no-ff <branch_name> # 采用no fast forward的合并方式,这种方式在合并的同时会生成一个新的commit
git merge --abort # 尽量回退到merge前的状态(可能会失败)
git merge --squash <branch_name> # 将目标分支合并过来但不携带commit信息,执行后最后需要提交一个commit(好处,代码整洁)
git重新基变(rebase):
git rebase <branch_name> #
git rebase --continue # 执行rebase出现冲突解决后,执行该命令会继续应用(apply)余下的补丁
git rebase --skip # 跳过当前提交
git rebase --abort # 终止rebase, 分支会回到rebase开始前的状态
git获取/拉(fetch/pull):
git fetch
git pull
git pull --rebase
git推(push):
git push origin master
git push -u origin master
git push -f origin
git push --all origin