DEV Community

100x.crypto πŸš€πŸš€
100x.crypto πŸš€πŸš€

Posted on

How to back up and sync your terminal configuration with a GitHub dotfiles repo

If you use Ubuntu (or any Linux system) regularly, chances are you've customized it over time, adding handy aliases, defining environment variables, tweaking your shell prompt, and installing useful tools.

These small changes make your system feel yours. But here is the problem:

πŸ’‘ What happens if your system breaks, gets wiped, or you switch to a new machine?

If all your configuration lives only on one device, you could lose hours β€” even days β€” of work rebuilding your environment from scratch.

The solution: Store your configuration files ("dotfiles") in a GitHub repository.
This not only backs them up but also makes it easy to sync them across devices and restore your setup instantly on a new machine.

This guide shows you how to do exactly that, step by step.

πŸš€ Step 1: Organize your config files

Let's say you would like to back up your ~/.bashrc and ~/.bash_aliases files (you can apply the same approach to any other config file).

First, create a dedicated folder within your home directory (~) to hold your dotfiles:

mkdir ~/dotfiles
Enter fullscreen mode Exit fullscreen mode

Now copy your config files into that folder:

cp ~/.bashrc ~/dotfiles/bashrc
cp ~/.bash_aliases ~/dotfiles/bash_aliases
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Tip: To view your dotfiles in your home directory (hidden by default), use ls -a.

⚠️ Security Warning:
Be very careful not to include sensitive files in your dotfiles repository β€” especially if it’s public.

❌ Never commit files like:

  • ~/.ssh/* (private SSH keys)
  • ~/.aws/credentials (cloud access keys)
  • .env files with API keys or passwords
  • GPG keys or database credentials

If someone gains access to these files, they could impersonate you, access your servers, or compromise your accounts.

βœ… Best practice:

  • Only track safe, reusable configuration files (like .bashrc, .bash_aliases, .gitconfig, etc.).
  • Add sensitive files to .gitignore so they’re never pushed by mistake.
  • Consider using a private repository if you absolutely must store sensitive configs β€” and even then, avoid committing secrets directly.

🧰 Step 2: Initialize a Git repository

Log in to your GitHub account in your browser and create a new repository called dotfiles. Leave it empty (i.e., don't initialize with a README or .gitignore). We'll add the files from our local machine shortly.

Now navigate to your newly created local dotfiles folder via

cd ~/dotfiles
Enter fullscreen mode Exit fullscreen mode

...and turn it into a Git repository:

git init
git add .
git commit -m "Initial backup of my shell config"
Enter fullscreen mode Exit fullscreen mode

Your configuration files are now tracked by Git locally.

πŸ’‘ Note: If you don't have Git installed or run into issues here, check out my other post on how to install Git and push your first repository.

πŸ™ Step 3: Push it to GitHub

Next, connect your local repository to the remote one you just created:

git remote add origin git@github.com:yourusername/dotfiles.git
Enter fullscreen mode Exit fullscreen mode

Now push your local files to GitHub:

git branch -M main
git push -u origin main
Enter fullscreen mode Exit fullscreen mode

If everything worked correctly, you should now see your bashrc and bash_aliases files listed in your GitHub repository.

πŸ§ͺ Step 4: Restore your setup on a new machine

Here’s where the magic happens. If you ever reinstall your OS or switch to a new device, you can bring your setup back with just a few commands:

git clone git@github.com:Walodja1987/dotfiles.git ~/dotfiles
cp ~/dotfiles/bashrc ~/.bashrc
cp ~/dotfiles/bash_aliases ~/.bash_aliases
source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Boom! Your terminal is back exactly how you left it.

πŸ’‘ Note on SSH vs HTTPS:
In the example above, I cloned the repo using the SSH URL (git@github.com:...), which lets Git authenticate automatically using your SSH key, without a password or token.

Once your SSH key is configured on GitHub, this is usually the easiest option. Check out my other post no how to set up the SSH key.

If you prefer to use HTTPS, you can do so, but GitHub no longer accepts passwords for Git operations. Instead, you'll need to use a Personal Access Token (PAT) as your password when prompted (you find the PAT in GitHub under Settings -> Developer Settings -> Personal access tokens -> Tokens (classic)):

git clone https://github.com/yourusername/dotfiles.git ~/dotfiles
Enter fullscreen mode Exit fullscreen mode
  • Username: your GitHub username
  • Password: your Personal Access Token (PAT)

Both methods work, but SSH is often smoother for day-to-day use, while HTTPS + PAT can be useful in scripts or CI/CD pipelines.

🧠 Step 5: Keep it up to date

Whenever you tweak your local dotfiles, just update your repository:

cp ~/.bash_aliases ~/dotfiles/bash_aliases
cp ~/.bashrc ~/dotfiles/bashrc
cd ~/dotfiles
git add .
git commit -m "Update aliases and bashrc"
git push
Enter fullscreen mode Exit fullscreen mode

This ensures your GitHub repo always mirrors your current setup and your latest configuration is always just a git clone away.

✨ Final Thoughts

Treating your shell configuration like code β€” version-controlled, backed up, and portable β€” is one of the most valuable habits you can develop as a developer or power user.

With a simple dotfiles repository on GitHub, your carefully crafted setup is safe, reproducible, and always within reach, no matter what happens to your system.

Note: I use ChatGPT to help me gather information and shape these posts, but I always test the commands myself and add my own insights to make them easier to understand. This blog is my personal record of what I learn as I go deeper into Linux. If it helps someone else too, even better!

Top comments (0)