Git 常用命令大全

2020-09-28  本文已影响0人  俄小发

初始化

git init
git init [project-name]
git clone [url]

配置

Git的设置文件为.gitconfig,它可以在用户主目录下(全局配置),也可以在项目目录下(项目配置)。

git config --local -l
git config --global -l
git config -e --global
git config -e --local
/* 设置全局,将--local改成--global */
git config --local user.name [name]
git config --lcoal user.email [email address]

美化和设置别名

git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
git config --global branch.autosetuprebase always
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.br branch
git config --global alias.cm 'commit -m'
git config --global alias.ca 'commit --amend'

仓库相关

git remote -v
git remote show [remote]
git remote add [shortname] [url]

分支相关

git branch [branch-name]
git checkout -b [branch]
git branch [branch] [commit]
git branch --track [branch] [remote-branch]
git branch --set-upstream [branch] [remote-branch]
git cherry-pick [commit]
git branch -a
git push origin --delete <分支名>
git branch -d <分支名>

提交远程

git add .
git commit -m [message]
git commit --amend -m [message]
git push origin <分支名>
git push origin <分支名> --force

同步远程

git fetch [remote]
git pull [remote] [branch]
相当于
git fetch [remote]
git merge [branch]
git pull --rebase [remote] [branch]
相当于
git fetch [remote]
git rebase [branch]

撤销

// 全部恢复,file为.,如git checkout .
git checkout [file]
git checkout [commit] [file]
git reset --soft [commit]
// 全部恢复,file为.,如git reset .
git reset [file]
git reset --hard
git reset [commit]
git reset --hard [commit]
git reset --keep [commit]
git revert [commit]

git revert 和 git reset的区别

  • git revert是用一次新的commit来回滚之前的commit,此次提交之前的commit都会被保留;
  • git reset是回到某次提交,提交及之前的commit都会被保留,但是此commit id之后的修改都会被删除

查看信息

git status
git log
git log --stat
git log --follow [file]
git whatchanged [file]
git log -p [file]
git blame [file]
git diff
git diff --cached [file]
git diff HEAD
git diff [first-branch]...[second-branch]
git show [commit]
git show --name-only [commit]
git show [commit]:[filename]
git reflog

标签

git tag
git tag [tag]
git tag -a [tag] -m 'message'
git tag -a [tag] -m 'message' [commit]
git show [tag]
git push origin [tag]
git push origin --tags
git checkout -b [branch] [tag]
git tag -d [tag]
git push origin :refs/tags/[tag]

Fork仓库

git remote add upstream <原仓库地址>
git reset --hard upstream/master
git pull --rebase upstream master
上一篇 下一篇

猜你喜欢

热点阅读