git基本操作命令总结--新手必备
文章目录
$ git init 创建git仓库
$ git add <filename> 将工作区文件添加进暂存区
$ git add -f <filename> 强制添加
$ git chech-ignore -v <filename> 检查.gitignore文件中规则与该文件的冲突
$ git commit -m <message> 暂存区文件提交至仓库,-m为此次提交的备注
$ git status 查看仓库当前状态
$ git diff <filename> 查看文件修改内容
$ git log 查看提交历史
$ git reflog 查看命令历史
$ git reset --hard <commit-id> 回退历史版本,HEAD指向当前版本,HEAD^表示上一个版本,上n个版本可以写成HEAD~n
$ git checkout -- <filename> 撤销工作区的修改,注意--。如文件为被提交至暂存区,文件将回退到与版本库一致,否者回退到与暂存区一致
$ git reset HEAD <filename> 撤销暂存区的修改
$ git rm <filename> 从版本库中删除该文件
$ git branch 查看分支
$ git branch <name> 创建分支
$ git checkout <name> 或$ git switch <name> 切换分支
$ git checkout -b <name>或$ git switch -c <name>创建并切换到分支
$ git branch -d <name> 删除分支
$ git branch -D <name> 强行删除分支
$ git merge <name> 将分支合并到当前分支上
$ git merge --no-ff -m "merge with no-ff" <name>同样是合并分支, 其中--no-ff参数表示禁用Fast forward模式,而此次合并会创建一次新的commit,加上-m参数可以添加commit表述
$ git stash 储存现有文件状态,保留工作现场
$ git stash list 查看stash保存记录
$ git stash apply 或$ git stash apply stash@{<n>} 恢复最近保存状态或指定的stash,但不会删除stash内容。<n>可以用git stash list查看
$ git stash drop 删除stash内容
$ git stash pop 恢复并删除stash内容
$ git cheey-pick <commit> 将某次提交到当前分支,避免重复劳动
$ git rebase 将本地末push的分叉提交历史整理成直线
与远程仓库相关指令
$ git remote add <repo-name> <server-path> 将本地仓库与远程库关联。git的远程仓库名称<repo-name>默认为origin,可自定义其他名称。
例如:$ git remote add origin git@github.com:michaelliao/learngit.git
$ git clone <server-path> 将远程库克隆到本地,一般只会克隆master分支
$ git checkout -b <branch-name> <repo-name>/<branch-name> 在本地建立与远程分支相对应的分支,本地与远程分支名一致方便管理
$ git push -u <repo-name> master 将master分支的所有内容推送到远程库,master可以替换成其他分支,需与远程库对应。
此后,可以使用命令$ git push origin master推送最新修改
$ git remote -v 查看远程库详细信息
$ git remote rm <repo-name> 删除远程库
$ git pull 抓取远程的最新提交
$ git branch --set-upstream <branch-name> <repo-name>/<branch-name> 建立本地分支与远程分支的关联,在提示no tracking information,则说明本地分支和远程分支的链接关系没有创建。
操作标签命令
$ git tag 查看标签
$ git tag <tagname> 在当前分支HEAD打上标签
$ git tag -a <tagname> -m <message> <commit-id> 在某次提示上打上带说明的标签,-m指说明文字,-a指定标签名,均可选用
$ git tag -d <tagname> 删除某个标签
$ git push <repo-name> :refs/tags/<tagname> 删除一个远程标签
$ git push <repo-name> <tagname> 将某个标签推送到远程
$ git push <repo-name> --tags 将全部标签推送到远程
注意:标签总是和某个commit挂钩。如果这个commit既出现在master分支,又出现在dev分支,那么在这两个分支上都可以看到这个标签。
改写配置文件
$ git config --global alias.<other-name> <order> 简写命令,例如:
$ git config --global alias.unstage 'reset HEAD'
当敲入命令:
$ git unstage test.txt
实际Git执行的是:
$ git reset HEAD test.txt
亦或者这样,用co表示checkout,ci表示commit,br表示branch:
$ git config --global alias.co checkout
$ git config --global alias.ci commit
$ git config --global alias.br branch
加上--global是针对当前用户起作用的,如果不加,那只针对当前的仓库起作用。
每个仓库的配置文件都放在.git/config文件中。
该文章为个人学习git后的一些学习总结,如果错漏,欢迎补充。更详细的内容可以到git教程,雪峰老师写的文档写的很棒,欢迎大家围观

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论