Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Git on Linux: Setup and First Steps

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Yes, you can get started with Git on Linux by installing it, configuring your identity, and learning the basic add/commit workflow. This guide walks you through installing Git, setting it up for the first time, and making your first commit — no prior version control experience needed.

What You'll Need

  • A Linux machine with internet access (for installing Git via your package manager)
  • A terminal (most distributions include one, like GNOME Terminal or Konsole)
  • Basic comfort with the command line (e.g., navigating directories with cd)
  • Optional: a GitHub, GitLab, or Bitbucket account if you plan to push to a remote repository later

Step-by-Step

  1. Install Git. Use your distribution's package manager. For Debian/Ubuntu: sudo apt update && sudo apt install git. For Fedora: sudo dnf install git. For Arch: sudo pacman -S git. After installation, verify with git --version.
  2. Set your identity. Git needs your name and email for every commit. Run these two commands, replacing the values with your own:
    git config --global user.name "Your Name"
    git config --global user.email "you@example.com"
  3. Check your configuration. Run git config --list to see your settings. This helps confirm that the previous step worked.
  4. Navigate to your project folder. Use cd /path/to/your/project to enter the directory where you want to track files. If you don't have a project yet, create one: mkdir myproject && cd myproject.
  5. Initialize a repository. Run git init. This creates a hidden .git folder that stores all version history. You only do this once per project.
  6. Add files to the staging area. Create a file (e.g., echo "Hello" > README.md) then run git add README.md or git add . to add all files in the current directory. Staging means "mark these changes for the next commit."
  7. Commit your changes. Run git commit -m "Initial commit". The -m flag lets you write a message inline. This saves a snapshot of the staged files.
  8. Check your status and log. Use git status to see what's staged/unstaged, and git log --oneline to view your commit history.
  9. Make a second change. Edit a file, then repeat the add and commit steps. This practice reinforces the workflow.
  10. Optional: Connect to a remote. If you have a GitHub/GitLab repo, add it with git remote add origin https://github.com/username/repo.git, then push with git push -u origin main (or master if that's your branch). You'll need to authenticate with a personal access token or SSH key.

Common Mistakes

  • Forgetting to configure user.name/user.email. Git will refuse to commit with a default message. If you see an error about identity, run the config commands from Step 2.
  • Using git add . without checking what's in the directory. This stages everything, including unwanted files like build artifacts or secrets. Use git status first.
  • Committing too often or too rarely. Commit logical units of work, not every keystroke.
  • Mixing up staging and committing. git add stages, git commit saves. You must do both in order.
  • Typing git commit without -m. This opens a text editor (like Vim). If you're not familiar with it, you might get stuck. Press Esc, then type :wq to save and quit, or use git commit -m "message" to avoid the editor.

Tips

  • Use a .gitignore file. Create a file named .gitignore in your project root to exclude files like node_modules/, *.log, or .env. Add entries one per line.
  • Write clear commit messages. Use the imperative mood: "Fix typo in README" rather than "Fixed typo" or "changes".
  • Learn git status early. It's your best friend for understanding what's happening.
  • Use git diff to see unstaged changes. Run git diff to view modifications before adding them.
  • Set up SSH keys for remote repos. This avoids typing your password every time. Use ssh-keygen and add the public key to your Git hosting account.

Troubleshooting

If Git is not found after installation: Your shell may not have refreshed the PATH. Close and reopen the terminal, or run source ~/.bashrc (or ~/.zshrc).

If git commit fails with "Please tell me who you are": You skipped Step 2. Run the two git config --global commands again, then retry the commit.

If you accidentally committed with the wrong email: Use git commit --amend --author="Your Name " to fix the last commit's author. This rewrites history, so only do it if you haven't pushed yet.

If you staged the wrong file: Use git reset HEAD filename to unstage it. Your changes remain on disk.

If git push asks for a username and password repeatedly: You're likely using HTTPS. Either use SSH (generate a key and add it to your hosting account) or use a personal access token instead of your account password. For GitHub, go to Settings > Developer settings > Personal access tokens.

If you're stuck in Vim after typing git commit without -m: Press Esc, then type :q! to quit without saving, then retry with -m.

Frequently Asked Questions

What is the difference between git fetch and git pull?

git fetch downloads changes from the remote but doesn't merge them. git pull does both fetch and merge. Use fetch when you want to inspect changes first.

How do I undo the last commit?

If you haven't pushed, use git reset --soft HEAD~1 to undo the commit but keep your changes staged. Use git reset HEAD~1 to unstage them too. Never reset after pushing unless you know what you're doing.

What is a branch and why should I use one?

A branch is a separate line of development. It lets you work on new features without affecting the main codebase. Create one with git branch feature-name and switch with git checkout feature-name (or git switch in newer Git).

How do I see all branches?

Run git branch to list local branches. The current branch is marked with an asterisk. Add -a to see remote branches too.

What is a merge conflict?

A merge conflict happens when two branches change the same line of a file differently. Git can't decide which to keep, so it marks the file with conflict markers. Edit the file to resolve, then git add and git commit.

Do I need to use GitHub to use Git?

No. Git works entirely locally. GitHub is a remote hosting service that adds collaboration features. You can use Git without any remote, but remotes are useful for backup and teamwork.