git-介绍与使用
个人学习整理的git相关知识
git
git
(读音为/gɪt/)是一个开源的分布式版本控制系统,可以有效、高速地处理从很小到非常大的项目版本管理。 也是Linux之父Linus Torvalds为了帮助管理Linux内核开发而开发的一个开放源码的版本控制软件
具体来说,就是用来管理代码项目的软件
作为一名软件开发者,肯定会有很多代码项目,这些项目就可以使用git
来提交至基于git
的代码存储平台如github
、gitee
、gitlab
等等,且可以管理它们的不同版本,还可以去克隆别的开发者的开源项目,拉取他们的代码学习,而且git
命令适用于所有基于git
的代码存储平台
综上,git
应是一名软件开发者的必备技能
安装
windows下安装非常方便,只需官网下载安装程序,按指引安装即可
安装完成后,打开cmd终端输入
git --version
出现相应git版本,说明安装成功
克隆远程仓库项目
# 根据远程仓库地址将项目克隆到本地,会生成隐藏的.git文件,该文件存储仓库的git相关信息
git clone https://git-repo-address.git
# 等价于
git init
git remote add origin https://git-repo-address.git
# 拉取远程仓库默认分支并且本地生成同名分支
git pull origin <default-branchname>
# 克隆远程仓库指定分支
git clone -b <branchname> https://git-repo-address.git
提交代码至远程仓库
本地代码初次提交至远程空仓库时需要运行git init
命令,创建必要的.git
文件即本地仓库,该文件默认是隐藏的,.git
文件中存储本项目相关git信息,如后续添加的远程仓库地址,每次提交内容的说明也就是git commit
等等信息
初次提交
# 创建.git文件即本地仓库
git init
# 保存项目内容至本地仓库
git add .
# 本次提交的说明
git commit -m "commitText"
git remote add origin https://git-repo-address.git
git push -u origin "master"
之后提交
git add .
git commit -m "commitText"
# 多人开发的项目每次提交前应先pull拉取代码
# 其他开发者可能比你先提交代码,不拉取直接提交会覆盖他的代码
git pull
# 拉取后没有代码冲突(不同开发者修改了相同一处的代码)可以直接提交
git push
如果出现代码冲突问题(相同代码处有不同的修改),这时需要手动在项目中选择保存哪一部分代码,从而解决冲突,然后重新走一遍提交流程
查看状态命令
# 显示文件修改情况,是否添加到本地仓库了
git status
.gitignore
为 npm 包添加.gitignore,文件内容加入,代码提交至仓库时就会跳过.gitignore 中的内容,如以下代码就会跳过 node_modules
node_modules
如果不想提交.gitignore,可以在.git/info/exclude 中加入以下代码排除它自身
.gitignore
分支操作
# 查看所有分支
git branch
# 查看远程所有分支
git branch -r
# 查看本地和远程的所有分支
git branch -a
# 新建分支
git branch <branchname>
# 删除本地分支
git branch -d <branchname>
# 删除远程分支
git push origin --delete <branchname>
# 重命名本地分支
git branch -m <oldbranch> <newbranch>
# 查看本地所有分支与远程分支的映射关系
git branch -vv
# 撤销本地当前分支与远程分支的关系
git branch --unset-upstream
# 建立本地当前分支与远程分支映射关系
git branch --set-upstream-to origin/<branchname>
# 切换分支
git checkout
# 切换并创建分支
git checkout -b <branchname>
# git参数解释:
-d --delete:删除
-D --delete --force 强制删除
-f --force:强制
-m --move:移动或重命名
-M --move --force 强制移动或重命名
-r --remote:远程
-a --all:所有
提交至远程分支
# 新建一个分支并切换至
git checkout -b <branchname>
# 会直接push到本地分支同名的远程分支
git add .
git commit -m "commitText"
git push
提交至远程新分支
# 新建一个分支并切换至
git checkout -b <branchname>
# 提交该分支至远程仓库
git push --set-upstream origin <branchname>
删除远程分支
# 方法一,推送空分支到远程仓库
git push origin :<branchname>
# 方法二,删除远程仓库分支
git push origin --delete <branchname>
管理远程仓库地址
# 查看远程仓库地址
git remote -v
# 添加远程仓库地址到本地仓库中,如已存在,会提示exist
git remote add origin https://git-repo-address.git
# 修改远程仓库地址
git remote set-url origin https://git-repo-address.git
# 删除远程仓库地址
git remote remove origin
查看修改本地用户名和邮箱
# 查看
git config user.name
git config user.email
# 修改
git config --global user.name "git-userName"
git config --global user.email "git-email"
查看与删除代理
# 查看全局http代理
git config --global http.proxy
# 查看全局https代理
git config --global https.proxy
# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy
shell脚本一键提交代码
在本地项目文件夹目录中,新建txt文件,加入以下内容,更改后缀为bat,点击即可运行,会将当前项目直接提交
#! /bin/bash
time=$(date "+%Y-%m-%d %H:%M:%S")
git add .
git commit -m ":memo:撰写文档 $time"
git push
fatal: refusing to merge unrelated histories
两个仓库有不同的开始点,也就是两个仓库没有共同的 commit,出现无法提交的情况
git pull origin main --allow-unrelated-histories
更多推荐
所有评论(0)