git的一些使用

git的一些使用

windows中使用git,并不是所有版本的windows都能默认安装和支持winget,需要win10的一定版本以上或者win11。如果没有默认的winget,可以从Microsoft store中安装,名为应用安装程序的应用,完成后就可以从powershell中使用winget了。

# 查看已经安装的程序列表,会发现一些程序都是winget的
winget list
# 安装git
winget install git

如果winget并不能愉快使用,那么需要自行下载windows版本的git。安装完成后在想要运行git的目录右键git bash。当然有图形界面的版本和GitHub desktop类似的软件也是一个好的选择,操作上也要直观。朋友们也可以尝试使用。

ssh-keygen 的介绍

如果不想每次使用git的时候都输入密码,那么需要在git的服务器上配置ssh公钥。生成密钥对的加密算法有好几种。下面的内容仅仅介绍对接github的使用方法,其他类似,内容参考《Github入门与实践》3.1节,微信读书有电子书。

  • 密钥算法 rsa 在ssh.com种对rsa的描述如下:

rsa - and old algorithm based on the difficulty of factoring large numbers. A key size of at least 2048 bits is better. RSA is getting old and significant advances are being made in factoring. Choosing a different algorithm may be advisable. It is quite possible the RSA algorithm will become practically breakable in the foreseeable future. All SSH clients support this algorithm.
rsa 的安全性是基于分解一个大数的难度。可预见的rsa将被破解。关于rsa的介绍引自知乎RSA加密算法简介与实现,或者可以参考wikipedia,链接。在ssh.com站点介绍的algorithm and key size 一节中可以发现ecdsa算法是一个好的选择。

  • 生成密钥
# rsa
ssh-keygen -t rsa -b 4096 
# ecdsa
ssh-keygen -t ecdsa -b 521
# 还可以在后面使用 -C 添加 commit
# window中生成的两个文件会出现在用户目录下的.shh文件
  • 将公钥添加到GitHub
    将产生的公钥添加到github具体操作可见《Github入门与实践》,测试添加是否成功,成功会出现欢迎信息。
# git 是服务器的git用户,github是域名
ssh -T git@github.com
  • 关于快捷的登陆
    用Host来取代User+Hostname。登陆不再需要输入ip或者域名。
cd ~/.ssh
# windows
ni config
./config
# linux
touch config
vim config
# 在其中添加以下内容
Host "host name"
	Hostnmae "host ip"
	User "user name"
	Port "port一般是22"
# 保存并退出,测试
ssh -T "host name"

开始工作的配置

关于git的配置文件说明参考自Git config配置。git的配置文件有三层,我们平常使用的是global配置,即用户配置。

# 对于当前仓库进行配置
# 首先创建一个仓库
mkdir storehouse
cd storehouse
git init
# linux 显示目录
ls -al
# windows 显示目录 如果不能看帮助文档可以使用Update-Help命令 -h表示显示隐藏文件
dir -h
# 对用户进行配置,可以在~/.gitconfig文件中查看具体配置信息
git config --global user.name "Firstname Lastname"
git config --global user.email "your_email@example.com"
git config --global color.ui auto
# 也可以这样查看
git config --global --list

对于远程仓库进行配置

# 增加远程的仓库,并将远程仓库用一个符号代替,这里使用的是origin
git remote add origin git@github.com:"your repository"
# 可以对远程仓库进行改名或者删除操作如下,这些更改都会在config而文件中有所体现
git remote rename origin "new name"
git remote remove origin

当然也可以直接从远程的服务器进行clone,这样就不需要以上的操作了。

git clone git@github.com:"your repository"
cd "repository"
# 需要先使用github在创建一个仓库
# 并不需要设置远程

push&pull

想要愉快的push,首先要把内容添加到本地仓库。

# 查看仓库状态,能看见工作的本地分支,和连接的远程分支,以及工作区的状态
git status
git add -A
git commit -m "your commit"
# push内容到你想要的分支上
git push origin main
# 如果出现问题
git pull

gitignore

# 如果没有.gitignore文件
touch .gitignore
# 这是gitignore,设置为local的excludesfile
git config --local core.excludesfile .gitignore
vim .gitignore

往往并不需要对git目录下所有的文件都进行管理。一些通用的框架,程序中间生成的输出,并不需要进行同步,这里只针对源码进行管理就可以了。
写入.gitignore的目录会被过滤掉,如果不想要过滤需要使用!,还可以使用正则表达式来过滤文件。也可以参考这篇文章link。添加到gitignore的文件后再使用git status可以发现过滤的文件的更改不会出现在工作区。

分支switch

如果有需要对分支进行更改,介绍内容为下。

# 创建新的分支
git branch "new branch"
# 或者可以使用,这条命令会创建新的分支,并且使这个分支进入工作状态
git checkout -b "new branch"
git checkout -b "local branch name" origin/"remote branch name"
# 切换工作分支命令,-可以切换回上次的分支
git checkout "branch name"
git checkout -
# 查看现有的分支,并且看哪个分支现在正在工作状态
git branch
git branch -a
# 删除分支,删除远程的分支
git branch -d "branch name"
# 将本地的分支push到remote的分支,当使用空的本地分支的时候,会将远端的分支删除
git push origin "local branch":"remote branch"
git push origin :"branch name"

合并分支

合并分支:

# 首先切换工作的分支
git checkout "branch name"
git merge --no-ff "branch name2"
# 这样就可以将branch name2合并到branch name中

时空之旅

# 查看log
git log 
git log --pretty=short
# set head
git reset --hard "目标点的哈希值"

另外

想要在服务器创建一个git仓库,然后本地clone下来,如果是创建bare的仓库,那么因为没有工作树,内容是不可见的。用git不一定合适,不如在本地用git维护,然后使用ssh将文件上传上去。

# 远端
mkdir "repository"
git init
git config deny.CurrentBranch ignore
# 本地, repository path 是 makdir 创建文件夹的路径
git clone "User ID@Host Ip:Repository Path"
# 或者本地
mkdir "local repository"
git init
git remote add origin "User ID@Host Ip:Repository Path"
git pull origin master
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值