git常用命令

  1. git init初始化本地仓库

  2. git add .添加/删除(所有)文件到暂存区

  3. git reset HEAD .重置暂存区的文件与上一次的提交(commit)保持一致,工作区文件内容保持不变。

  4. git status查看git此时的状态

  5. git commit -m 'message'提交到本地仓库

  6. git remote add origin Address绑定远程仓库

  7. git pull origin main/master拉取同步远程服务器代码,无冲突直接合并,有冲突需要手动合并

  8. git push origin main/master推送至远程仓库

  9. git log查看提交记录

  10. git reflog操作记录(查看版本号)

  11. git reset --hard HEAD^/版本号回退上一个版本/某个版本(HEAD表示当前版本 ^表示上一个)

git多人协作

  1. git branch -a查看所有分支
  2. git checkout -b aaa创建新的分支(-b)aaa
  3. git checkout aaa切换到aaa分支
  4. git merge aaa(master)分支aaa合并到master
  5. git push origin aaa推送aaa分支到远程仓库aaa分支
  6. git push origin master:aaa推送master到远程的aaa分支
  7. git branch -d aaa删除aaa分支

git远程仓库更换名称,本地修改

这种情况适用于本地和远程的代码没得任何问题,就是远程仓库改了个名称,直接在本地修改远程仓库地址即可:

git remote set-url origin newAddress

另外还可以先删除,然后添加地址:

git remote rm origin

git remote add origin newAddress

绑定GitHub

打开 Git Bash,输入 ssh-keygen -t rsa 命令,表示我们指定 RSA 算法生成密钥,然后敲四次回车键,之后就就会生成两个文件,分别为秘钥 id_rsa 和公钥 id_rsa.pub.(注意:git中的复制粘贴不是 Ctrl+C 和 Ctrl+V,而是 Ctrl+insert 和 Shift+insert.)文件的位置在 Git Bash 上面都有显示,默认生成在以下目录:

  • Linux 系统:~/.ssh
  • Mac 系统:~/.ssh
  • Windows 10 :C:/Users/ASUS/.ssh

把公钥 id_rsa.pub 的内容记事本打开复制添加到 GitHub。

同时使用GitHub和Gitee

1. 删掉全局配置

git config --global --list
$ git config --global --unset user.name "你的名字"
$ git config --global --unset user.email "你的邮箱"

2. 为不同账户配置ssh秘钥

cd ~/.ssh 														# cd到当前用户的.ssh文件夹
ssh-keygen -t rsa -f ~/.ssh/id_rsa.gitee -C "注册gitee邮箱" #为不同秘钥指定名称
ssh-keygen -t rsa -f ~/.ssh/id_rsa.github -C "注册github邮箱"

完成后会在~/.ssh / 目录下生成以下文件:

  • id_rsa.github
  • id_rsa.github.pub
  • id_rsa.gitee
  • id_rsa.gitee.pub

复制公钥分别在github和gitee中设置

cat id_rsa.github.pub
cat id_rsa.gitee.pub

添加新的私钥

$ ssh-agent bash
$ ssh-add ~/.ssh/id_rsa.github
$ ssh-add ~/.ssh/id_rsa.gitee

3. 进行全局配置

touch ~/.ssh/config    

# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa.gitee

# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa.github

Host 它涵盖了下面一个段的配置,我们可以通过他来替代将要连接的服务器地址。 这里可以使用任意字段或通配符。 当ssh的时候如果服务器地址能匹配上这里Host指定的值,则Host下面指定的HostName将被作为最终的服务器地址使用,并且将使用该Host字段下面配置的所有自定义配置来覆盖默认的/etc/ssh/ssh_config配置信息。

Port 自定义的端口。默认为22,可不配置

User 自定义的用户名,默认为git,可不配置

HostName 真正连接的服务器地址

PreferredAuthentications 指定优先使用哪种方式验证,支持密码和秘钥验证方式

IdentityFile 指定本次连接使用的密钥文件

4. 测试连接

ssh -T [email protected]
Hi yourname! You've successfully authenticated, but GitHub does not provide shell access.
ssh -T [email protected]
Hi yourname! You've successfully authenticated, but GITEE.COM does not provide shell access.

5. hexo博客仓库

vi .depoly_git/.git/config 增加

[user]
name = username
email = email

6. 针对不同的项目仓库

增加本地配置,在每个仓库的.git/config中进行配置不同的用户,以及其他的配置信息

$ git config --local user.name 'github/gitee账号名'
$ git config --local user.email 'github/gitee账号邮箱'

–global是在全局配置文件中设置

–local 是针对当前仓库的项目进行设置

更新hexo blog

hexo clean   #清除缓存文件 db.json 和已生成的静态文件 public
hexo g #生成网站静态文件到默认设置的 public 文件夹(hexo generate 的缩写)
hexo s #启动本地服务器预览网页
hexo d #自动生成网站静态文件,并部署到设定的仓库(hexo deploy 的缩写)

项目打包dist文件commit出错

检查后发现格式不符合标准,git不让上传,为了避免格式检查用
git commit -m "dist1" --no-verify