You are not logged in.

#1 2024-04-06 15:34:39

barboa
Member
Registered: 2024-04-04
Posts: 1

.dotfiles with git trouble

forgive me it has been a minute since I used git.

I want to create a git for my dotfiles so I can have them easily backed up. I am following the instructions listed here

https://wiki.archlinux.org/title/Dotfiles

 $ git init --bare ~/.dotfiles
$ alias dotfiles='/usr/bin/git --git-dir="$HOME/.dotfiles/" --work-tree="$HOME"'
$ dotfiles config status.showUntrackedFiles no

After running this, I have the git set up but it is not attached to a repository. When I make the repository on github, I come back to the CLI and try to do a commit.

I am faced with this.

fatal: this operation must be run in a work tree

I do

$ git remote  add origin <my repo URL>
$ git branch -M main
$ git push -u origin main 

git adds the remote origin fine but then I cant push to main or even master.

If you are so inquired to help thank you and please let me know if there is specific information you need.

Last edited by barboa (2024-04-06 15:35:20)

Offline

#2 2024-04-06 16:42:46

Brocellous
Member
Registered: 2017-11-27
Posts: 146

Re: .dotfiles with git trouble

You need to use the worktree argument every time with the bare repo, because it otherwise does not have a worktree. The intent is to treat $HOME like your worktree for the purpose of your dotfiles, but without making all git operations do so automatically. A bare repository is just the git dir separated from the work tree.

For example, I have:

$ git init --bare ~/.dotfiles
$ alias .git='git --git-dir=$HOME/.dotfiles --work-tree=$HOME'

The point of the alias is to use it when you are running operations on your dotfiles repo.

$ .git config --edit
$ .git diff -- ~/.config
$ .git add ~/.zshrc
$ .git commit -m 'zsh: new aliases'
$ .git ls-files
$ .git push

Etc.

Note that while it is safe if you use git properly, git is not as protective of untracked files as it is the tracked or ignored files. I prefer to blanket ignore everything in my dotfiles repo so that git operations never accidentally touch files I didn't intend. git will still operate properly on tracked files even if they match an ignored pattern. You cannot use ~/.gitignore for this purpose, because that is a user global ignore file. Instead you can use the exclude file within the git dir:

$ echo '*' >> ~/.dotfiles/info/exclude
$ .git add -f ~/.config/blah # -f flag is now necessary when adding previously untracked files. You can use the -u flag to add updated versions of already tracked files.

Offline

Board footer

Powered by FluxBB