git别名:
gitconfig–globalalias.cocheckout
git config –global alias.ci commit
gitconfig–globalalias.ststatus
git config –global alias.br branch
git lg高级配置
$ git config –global alias.lg “log –graph –pretty=format:’%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset’ –abbrev-commit –date=relative”
修改代码
修改代码之后没有提交,临时切换分支打包或者修改bug,这么处理:
gitstash把当前分支所有没有commit的代码先暂存起来
git stash list 暂存区记录
gitstashapply切换回来,发现原来修改未提交的代码还原
git stash drop 把最近一条的 stash 记录删除了
标签
列出所有tag
gittag新建一个tag在当前commit
git tag [tag]
新建一个tag在指定commit
gittag[tag][commit]查看tag信息
git show [tag]
提交指定tag
gitpush[remote][tag]提交所有tag
git push [remote] –tags
新建一个分支,指向某个tag
$ git checkout -b [branch] [tag]
切换分支
列出所有远程分支
gitbranch−r列出所有本地分支和远程分支
git branch -a
新建一个分支,但依然停留在当前分支
gitbranch[branch−name]新建一个分支,并切换到该分支
git checkout -b [branch]
查看变化
显示暂存区和工作区的差异
gitdiff显示暂存区和上一个commit的差异
git diff –cached [file]
显示工作区与当前分支最新commit之间的差异
gitdiffHEAD显示两次提交之间的差异
git diff [first-branch]…[second-branch]
显示某次提交的元数据和内容变化
$ git show [commit]
远程同步
下载远程仓库的所有变动
gitfetch[remote]显示所有远程仓库
git remote -v
显示某个远程仓库的信息
gitremoteshow[remote]增加一个新的远程仓库,并命名
git remote add [shortname] [url]
取回远程仓库的变化,并与本地分支合并
gitpull[remote][branch]上传本地指定分支到远程仓库
git push [remote] [branch]
强行推送当前分支到远程仓库,即使有冲突
gitpush[remote]–force推送所有分支到远程仓库
git push [remote] –all
推送当前某个分支到指定新仓库。如:将branch_a的整个数据(包括提交历史)全部提取出来,并建立一个新的仓库repo_b
$ git remote add origin_repo_b git@server_ip:/path/repo_b.git
git push origin_repo_b branch_a
撤销
恢复暂存区的指定文件到工作区
gitcheckout[file]恢复某个commit的指定文件到工作区
git checkout [commit] [file]
恢复上一个commit的所有文件到工作区
gitcheckout.重置暂存区的指定文件,与上一次commit保持一致,但工作区不变
git reset [file]
重置暂存区与工作区,与上一次commit保持一致
gitreset–hard重置当前分支的指针为指定commit,同时重置暂存区,但工作区不变
git reset [commit]
重置当前分支的HEAD为指定commit,同时重置暂存区和工作区,与指定commit一致
$ git reset –hard [commit]