Skip to content

Instantly share code, notes, and snippets.

@shreewatsa
Created January 19, 2024 02:40
Show Gist options
  • Save shreewatsa/6bbb1c86657f8da8d4379785e16bb1d8 to your computer and use it in GitHub Desktop.
Save shreewatsa/6bbb1c86657f8da8d4379785e16bb1d8 to your computer and use it in GitHub Desktop.
Dotfiles (Managing and version controlling of dotfiles)

Create a bare repo and push to Github.

1. Create an empty repo in GitHub (without README, .gitignore, Licence)

2. Create a bare repo.

cd ~;
git init --bare ~/dotfiles;
echo 'alias bare="/usr/bin/git --git-dir=$HOME/dotfiles --work-tree=$HOME"' >> ~/.bashrc;
source ~/.bashrc;
bare config --local status.showUntrackedFiles no;

3. Add config files and Push the bare repo to Github.

bare add ~/any_folder/any_file.sh;
bare status;
bare commit -m "Added some configs";
bare branch -M master;  # Rename the current branch to "master"
bare remote add origin git@github.com:shreewatsa/dotfiles.git;
bare push -u origin master;

Clone the repo on a new machine.

echo "dotfiles" >> .gitignore;
echo 'alias bare="/usr/bin/git --git-dir=$HOME/dotfiles --work-tree=$HOME"' >> ~/.bashrc;
source ~/.bashrc;
bare config status.showUntrackedFiles no
git clone --bare git@github.com:shreewatsa/dotfiles.git $HOME/dotfiles;
bare checkout;  # If conflict with existing files, either remove/rename/move those existing files.

You can automate above like this:

#!/bin/bash
git clone --bare git@github.com:shreewatsa/dotfiles.git $HOME/dotfiles;
function bare {
   /usr/bin/git --git-dir=$HOME/dotfiles/ --work-tree=$HOME $@
}
bare checkout
if [ $? = 0 ]; then
  else
  mkdir -p .config-backup
  bare checkout 2>&1 | egrep "\s+\." | awk {'print $1'} | xargs -I{} mv {} .config-backup/{}
  bare checkout
fi;

You can even create a gist of this snippet, then curl and run the snippet like this:
$curl -Lks <gist/snippet url> | /bin/bash;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment