git基本使用

📅 2026/7/30 10:51:33
git基本使用
概述git是分布式的版本控制工具安装* 官网https://git-scm.com* documentbook1.5getting startedmac 环境变量配置* 修改环境变量 vim ~/.bash_profile* 让环境变量生效 source ~/.bash_profile配置详解目的是 告诉git 你是谁查看git 所有命令git config级别* global: 一台电脑中的一个用户 推荐* system 对应一台电脑* local: 每个项目下的具体配置 (默认)配置//配置用户名git config --global user.name 你的git用户名//配置邮箱git config --global user.email 你的git账户邮箱//配置高亮git config --global color.ui true//配置编辑器git config --global core.editor vim//查看配置信息 --global的配置会放到 ~/.gitconfig文件中cat ~/.gitconfig//system 的配置 会放到 /etc/.gitconfig//local 会放到每一个项目当中的 .git/config文件比如在当前目录下 git init就会在当前目录下多一个 .git/config的配置文件git基本工作流程* 初始化 git init* 查看状态: git statusUntracked files: 没有追踪到的文件index.php* 添加git-stating-area: git add 文件名将文件变化提交到缓存区stating-area类似于缓存区为下一次commit做准备teacher-zhou:git yu$ git add .teacher-zhou:git yu$ git statusOn branch masterYour branch is up-to-date with origin/master.Changes to be committed:(use git reset HEAD file... to unstage)new file: .gitignorenew file: git基本使用.md* 添加到版本: git commit* 查看提交信息: git log可以看到一个hash值这个值是当前版本的标识可以用他来回复版本到这个点使用 git reset --hard 哈希值### commit 出错了的解决方案git commit --amend 修正错误的版本1已经提交到了github或者是团队自建的git服务器即代码已经在团队公开了。解决方案 修改文件 重新 git add index.php git commit -m修改 index.php上次提交的错误,来覆盖上次错误的版本git add .git commit -mfixed2 没有push到服务器仅仅是在本地的版本库中时发现错了解决方案 使用 git reset --hard(恢复到哈希值所在的版本) 哈希值git reset --hard 哈希值 绝对恢复到这个版本相当于穿越回过去。 回到18岁的时候年龄也回18岁了然后修改文件到你想要的样子git commit --amend 修正版本git reset --soft 哈希值 带着现在的改变回到过去的时间节点。 带着现在的年龄回到18岁那一年忽略不必要的文件* .gitignoregit分支理解与应用git branch about 创建 about分支git branch 查看当前有几个分支git checkout about 切换到about分支git merge about 合并about分支到 当前分支 上git branch -d about 删除about分支* 通过git分支同时做几件不同的事比如你之前写的一个模块提交上去了。在开发第二个模块开发一半的时候发现上一个版本有问题需要修复但是新功能做了一半。这个时候其实每一个新功能都开一个分支是很好的选择相互不会影响* git checkout -- ./index.js 把index.js恢复到上一个版本源文件 index.js 已经在主分支上有一个版本开发about页面时创建一个新的分支 git branch about,切换到about分支git checkout about,开发about的新代码。发现原来master分支上index.js有错误那么切换回master分支。git checkout master,获得master分支上 原来的index.js并做修改,git checkout -- ./index.js. 在切换回 about分支继续开发about的功能合并分支处理冲突* git checkout -b new-design 创建分支new-design并进入该分支* 在master分支下创建 index.html文件并提交到版本库* 在new-design分支下修改index.html中的title* 在master分之下也修改同一个位置 title* 在master分之下合并 new-design分支会提示冲突teacher-zhou:git yu$ git merge new-designAuto-merging index.htmlCONFLICT (content): Merge conflict in index.htmlAutomatic merge failed; fix conflicts and then commit the result.* 修改冲突内容确定保留的内容后重新 add 冲突文件,重新commitgit add index.htmlgit commit index.html* 在做一次合并git merge new-design---Git alias 快捷方式* 添加快捷方式git status 》 git sgit config --global alias.s status* 删除 git status 快捷方式git config --global --unset alias.sgit stash将当前工作的文件暂存起来然后需要的时候在拿回来。应用场景 当你在一个新的分支上开发新功能时开发到一半突然有一个紧急的bug需要修复那么我们可以把当前分支开发的内容暂存起来 git stash.然后切换到主分支 git checkout master.去做其他事做好之后切回新功能开发分支,执行git stash pop拿回刚才写了一半的内容继续工作* git stash 可以理解为将工作台做了一半的内容暂存* git stash pop: 拿回原来工作区的内容* git stash list: 查看工作台有哪些临时存储内容