Skip to content

1.Github推代码

  1. 生成ssh

    bash
    ssh-keygen -t rsa -b 4096 -C "sunminghong110@gmail.com"  # 生成SSH密钥
    eval "$(ssh-agent -s)" 
    ssh-add ~/.ssh/id_rsa  # 将SSH代理添加到ssh-agent
    cat ~/.ssh/id_rsa.pub  # 查看并复制您的SSH密钥
  2. 本地拿到了公钥,接下来就需要将公钥拷贝到远程仓库的相关配置中

    image-20240110133655598

  3. 进入项目路径

  4. 如果之前已经添加了远程仓库地址,那么你应该首先把之前的仓库地址干掉:

bash
# 如果你之前已经添加了远程仓库地址,那么你应该首先把之前的仓库地址干掉
git remote remove origin

# 然后再重新配置
git remote add origin ssh
  1. 如果第一次则初始化
bash
git init
git remote add origin ssh
  1. 提交代码

    bash
    git add .
    git commit -m 'name'
  2. 提交到仓库

    bash
    git push origin main  #原先是master,改了

2.推代码报错

  1. 删除本地的.git文件

    bash
    rm -rf .git
    ls -a .git #查看.git文件是否存在,如不存在重新初始化
  2. 重新初始化后报错main分支错误,将main分支代码拉出,后重新push

    bash
    git pull origin main

3.仍报错,可选择强制push

bash
git push -f origin main #有风险谨慎使用

3.拉特定版本代码

  1. 在终端或命令提示符中,使用 cd 命令进入要存储代码库的本地目录。

  2. 使用 git clone 命令将代码库克隆到本地目录中。例如:

    bash
    #克隆feature/user这个分支的代码
    git clone -b feature/user git@github.com:hongDa27/LuffyCity.git 
    
    #不加分支默认克隆main的代码
    git clone git@github.com:hongDa27/LuffyCity.git
  3. 使用 git log 命令查看代码库的提交历史。例如:

    bash
    git log
  4. 使用 git checkout 回滚到所需版本。例如:

    bash
    git checkout <commit id>   #黄色的是commit id

4.打版本号

打版本号,打tag,打标签:

1.查看标签:

bash
git tag

2.创建标签:

bash
git tag <标签>
git tag v1.1.0              
#或
git tag -a v1.1.0 -m <>

3.推送标签:

bash
git push origin --tags     
#或
git push origin v1.1.0

4.查看标签的版本信息:

bash
git show <标签> 
#如:
git show v1.1.0

5.补打标签:

bash
git tag -a v1.1.0 9fbc3d0

6.删除本地标签:

bash
git tag -d v1.1.0

然后推送一个空的同名标签到远程达到删除标签:

bash
git push origin :refs/tags/v1.1.0

7.获取远程版本:

bash
git fetch origin tag v1.1.0