安装:
| $ git config --global user.name "Your Name" |
| $ git config --global user.email "email@example.com" |
常用命令
| |
| git clone git@github.com:xxx/xxx.git |
| |
| |
| git add xxx |
| |
| |
| git commit -m |
| |
| |
| git push |
| |
| |
| git pull |
| |
| |
| git init |
本地使用:
notepad编辑器:https://notepad-plus-plus.org/
Git bash常用命令:
- mkdir:新建文件夹
- cd + 路径:前往文件夹
- pwd:显示当前目录
- ls:显示当前目录文件
- cat + 文件:查看文件内容
- rm + 文件:删除文件
第一步:把一个目录变成git仓库
第二步:往仓库里添加文件
第三步:把文件提交到仓库
| $ git commit -m “add ex31.py” |
说明:commit一次可以提交多个文件;-m后面是本次提交的说明
其他:
| $ git diff [filename] |
| $ git diff HEAD -- [filename] //查看工作区与版本库的区别 |
| $ git reset --hard HEAD^ |
| $ git reset --hard 1094a |
| $ git checkout -- filename |
| $ git reset HEAD filename |
- 若使用git rm删除了文件还没有commit修复办法
| git restore --staged <filename> //在暂存区修复文件 |
| git restore <filename> //修复文件即可 //或者git checkout <filename> |
其他概念:
git add 是把所有修改的部分放到暂存区内,git commit 是把暂存区内的所有内容提交到分支上。
由本地同步到远程
由于本地git仓库和远程git仓库是通过SSH加密的,所以需要蛇者SSH秘钥:
1:在本地创建SSH key:
| $ ssh-keygen -t rsa -C "youremail@example.com" //回车就好,然后在主目录里会生成.ssh文件夹,里面有公钥和私钥 |
| |
| |
2:登录github账户,添加SSH key的公钥,这样github就知道是不是本人提交的
3:在github创建仓库
4:将本地仓库同步到远程仓库(先建立本地仓库再建立远程仓库)
| $ git remote add origin https: |
| |
| $ git push -u origin master |
5:更改后更新远程库
注意:若git remote add origin xxx出现 fatal: remote origin already exists. 先删除再add
另外一种从远程仓库克隆到本地
1:先在github建立一个仓库
2:从远程仓库克隆一个到本地
| $ git clone git@github.com:yourname/name.git |
分支管理
| $ git checkout -b dev |
| |
| $ git branch dev |
| $ git checkout dev |
| $ git branch |
| * dev |
| master |
| $ git merge dev |
| |
| $ git merge --no-ff -m "merge with no-ff" dev |
注意:当master分支和其他分支有冲突的时候,git无法自动合并需要手动修改文件内容再合并
| $ git commit -m "conflict fixed" |
| $ git log --graph --pretty=oneline --abbrev-commit //查看分支合并图 |
| $ git log --pretty=oneline --abbrev-commit //查看分支commit id |
临时分支管理
当前分支工作只进行到一部分时,git可以先将当前的工作保存,去进行另外的工作
| $ git stash |
| $ git stash list |
| $ git stash apply stash@{0} |
| $ git stash pop |
| $ git stash apply |
| $ git stash drop |
| $ git cherry-pick <commit> //复制一个特定的提交到当前分支 |
| $ git branch -D <branch> //强行删除 |
多人协作
| $ git remote |
| $ git remote -v //查看远程库详细信息 |
| $ git remote rm origin //删除已关联的远程库 |
| //同时关联多个远程库,需要设置不同的远程库名字 |
| $ git remote add github git@github.com:name/name.git |
| $ git remote add gitee git@gitee.com:name/name.git |
| $ git push origin master |
| $ git push origin dev |
| $ git clone git@github.com:name/name.git //克隆已有分支 |
| $ git checkout -b dev origin/dev //在远程上创建新分支dev |
| $ git pull |
| $ git branch --set-upstream-to=origin/dev dev |
| |
标签管理
| $ git tag <name> |
| $ git tag <name> <commit id> //给某个commit打标签 |
| $ git tag -a <name> -m "tag information" <commit id> //打标签并添加标签信息 |
| $ git tag |
| $ git show <tagname> |
| $ git push origin <tagname> |
| $ git push origin --tags |
| $ git tag -d <tagname> //先删除本地标签 |
| $ git push origin :refs/tags/<tagname> //再删除远程标签,也是推送 |
忽略特殊文件
提交.gitignore文件到git,在.gitignore文件中写入要忽略的文件名或者文件夹或者文件规则。
| $ git add -f <filename> |
| $ git check-ignore -v <filename> //查看是哪条忽略规则忽略了 |
配置命令别名
| $ git config --global alias.co checkout |
| $ git config --global alias.ci commit |
| $ git config --global alias.br branch |
| $ git config --global alias.unstage 'reset HEAD' |
| $ git config --global alias.last 'log -1' |
| 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" |
注意:如果不使用--global参数,配置只对当前仓库有用,每个仓库的配置文件在.git/config文件中。
如果使用global参数,对此用户所有仓库有用,用户的配置文件在用户主目录下.gitconfig文件中。
配置别名也可以直接修改配置文件。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?