Obsidian笔记同步——基于Git方法

8 篇文章 1 订阅
1 篇文章 0 订阅

于gitlab或github等代码托管平台新建一个新的Repo

1.该repo目前是空仓库的状态,最好设置为私人仓库
2.尽量使用SSH

于本地笔记的文件夹内git bash here

git init
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

请注意,–global是全局配置,如果只想特殊化本仓库,可以去掉–global

git config --global user.name
git config --global user.email

查询当前仓库的配置信息

git branch -m <old-branch-name> <new-branch-name>

远程仓库主分支多为main,本地分支多为master,我们统一采用main命名

SSH配置

  • 在 GitHub 网站上,登录你的账户,然后转到 “Settings”(在头像旁边的下拉菜单中)。

  • 在左侧菜单中,点击 “SSH and GPG keys”。

  • 点击 “New SSH key”。

  • 将你复制的 SSH 公钥粘贴到 “Key” 字段中,为这个密钥起一个适当的标题,然后点击 “Add SSH key”。

git remote add origin

git remote add origin git@github.com:<username>/<reponame>.git

这一步将本地仓库与远程仓库相关联,但本地分支和远程分支没有共同的祖先分支,git push时往往会遇到如下报错:

The current branch main has no upstream branch. 
To push the current branch and set the remote as upstream, 
use git push --set-upstream origin main
To have this happen automatically for branches without a tracking upstream, see 'push.autoSetupRemote' in 'git help config'.
git push --set-upstream origin main

这一步将本地分支main和远程分支main相关联

你可能还会遇到如下问题:

$ git push --set-upstream origin main 
To git.acwing.com:<username>/<your repo name>.git
! [rejected] main -> main (fetch first) 
error: failed to push some refs to 'git.acwing.com:tom/cs.git' 
hint: Updates were rejected because the remote contains work that you do 
hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes 
hint: (e.g., 'git pull ...') before pushing again. 
hint: See the 'Note about fast-forwards' in 'git push --help' for details. 

这个错误通常发生在你试图将本地分支推送到远程分支时,远程分支已经有新的提交。你需要先将远程仓库的变更合并到你的本地分支,然后再推送你的更改。以下是解决这个问题的步骤:

git pull origin main

如果还有问题?

git pull origin main
From
git.acwing.com:tom/cs
* branch main -> FETCH_HEAD 
* fatal: refusing to merge unrelated histories

这个错误是因为你尝试从远程分支 main 拉取变更时,发现两个分支的历史记录没有共同的祖先,被认为是没有关联的历史。通常情况下,这会在你尝试合并两个不同的 Git 仓库或者两个不同的分支时发生。要解决这个问题,你可以使用 git pull 命令的 --allow-unrelated-histories 参数,或者通过手动创建一个新的分支来处理。以下是两种方法的说明:

1.Method 1

git pull origin main --allow-unrelated-histories

这会将远程分支的变更合并到你的本地分支,即使两个分支的历史无关。

2.Method 2

在本地创建一个新的分支:

git checkout -b new-branch-name
git pull origin main

问题本源—本地分支也要和远程分支关联

本地仓库与远程仓库相关联以后,是不是本地分支和远程分支还要关联?如果要关联,那又如何关联?

是的,本地仓库与远程仓库相关联后,本地分支和远程分支还需要进行关联。关联本地分支和远程分支有两个主要目的:

  1. 建立追踪关系(上游关系):关联后,你可以更轻松地使用 git pullgit push 命令,而无需显式指定远程分支的名称。

  2. 便于协作:在多人协作的项目中,通过关联分支,可以更好地跟踪和管理每个分支的变更。
    以下是如何关联本地分支和远程分支的步骤:

  3. 首先,确保你已经克隆了远程仓库到本地,或者将本地仓库关联到了远程仓库。

  4. 切换到你想要关联的本地分支。假设你想要关联的是 main 分支。

    git checkout main
    
  5. 关联本地分支和远程分支。使用 -u--set-upstream-to 参数,将本地分支与远程分支关联。
    如果远程分支与本地分支同名:

    git branch -u origin/main
    

    如果远程分支与本地分支不同名,例如你在本地有 main 分支,但远程分支是 origin/development

    git branch -u origin/development
    

    以上命令会将本地分支与指定的远程分支关联起来。

  6. 之后,你就可以使用 git pullgit push 命令,Git 会自动知道你要将变更推送到哪个远程分支,或从哪个远程分支获取变更。

在Obsidian中手动备份

Ctrl + P 打开命令面板,尔后输入 ogcb
ogcb 即 obsidian git create branch 将按序执行commit 和 push 操作

  • Backup
    • Create Backup: Commits all changes. If “Push on backup” setting is enabled, will also push the commit.
    • Create Backup with specific message: Same as above, but with a custom message
    • Create backup and close: Same as Create Backup, but if running on desktop, will close the Obsidian window. Will not exit Obsidian app on mobile.
      参考地址:Obsidian-Git文档

The end.

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云之君若雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值