本节主要记录一些git学习的笔记内容

git学习链接

git分支管理

  1. 新建分支 git checkout -b branchname
  2. 切换分支 git checkout branchname
  3. 查看分支 git branch
  4. 查看所有分支 git branch -av

比如你现在在dev,克隆下来的是主分支(main)

如果你要进行什么修改,可以建一个新的分之,比如按照今天的日期

git checkout -b 190721
# 创建并切换分支
git checkout 190721
# 直接切换分支

当系统重装后,如何连接git和github

github官方参考链接

先下载git

sudo apt-get git # deepin
sudo pacman -S git #manjaro

然后为本地 git 配置全局的 user 与 email 参数。

git config ‐‐global user.name "your github account name"
git config ‐‐global user.email "your github account email"

为了在后续操作中我们能将本地仓库的代码推送至 github 的仓库上,我们需要在本地生成 SSH 秘钥,并将公钥保存到 github 账户信息中,这样我们在本地提交的时候 github 就能通 过本地的私钥与公钥进行校验。

ssh‐keygen ‐t rsa ‐C "your github account email"

复制当前主目录下~/.ssh/id_rsa.pub,只需要把公钥的内容提交给 github 。

进入Github的个人setting 界面,选择 SSH and GPG keys ,点击 New SSH key ,在展开的窗口中填写密钥信息,title 可以随意,方便自己管理即可,key 那一栏则把刚刚生成的 id_rsa.pub 的内容复制进去。最后点击按钮添加。

测试是否连接到github

ssh -T git@github.com

问题一

$ ssh -vT git@github.com
> ...
> Agent admitted failure to sign using the key.
> debug1: No more authentication methods to try.
> Permission denied (publickey).

采取措施

# star图片t the ssh-agent in the background
eval "$(ssh-agent -s)"
> Agent pid 59566
ssh-add
> Enter passphrase for /home/you/.ssh/id_rsa: [tippy tap]
> Identity added: /home/you/.ssh/id_rsa (/home/you/.ssh/id_rsa)

问题二

permissions 0777 for '~/.ssh/id_rsa' are too open

采取措施

chmod -R 700 id_rsa id_rsa.pub known_hosts passwd

版本回退

  • git reset --hard HEAD^ 回退到上一版本
  • git reset --hard HEAD^^ 回退到上上版本
  • git reset --hard HEAD~100 回退到上100个版本
  • git reset --hard 具体版本号 回退到具体版本号
  • git reflog 记录每一次命令
  • git checkout -- readme.txt 命令git checkout -- readme.txt意思就是,把readme.txt文件在工作区的修改全部撤销,这里有两种情况:
    • 一种是readme.txt自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;
    • 一种是readme.txt已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。
    • 总之,就是让这个文件回到最近一次git commitgit add时的状态。

Git添加远程分支

# create a new repository on the command line
echo "# hugo-blog-theme" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/oxygenbytes/hugo-blog-theme.git
git push -u origin main

# push an existing repository from the command line
git remote add origin https://github.com/oxygenbytes/hugo-blog-theme.git
git branch -M main
git push -u origin main