sodcalculator

10 Tech How-To

How to Use Git in Linux: A Practical Guide

Guide 10-49673 · Updated · Step by step

This guide walks you through using Git on a Linux system, from your first commit to branching, merging, and pushing to a remote. You’ll learn the essential commands, common pitfalls, and how to fix mistakes. No prior Git experience is needed, but you should be comfortable with the Linux terminal.

What You’ll Need

  • A Linux system (any mainstream distribution, e.g., Ubuntu, Debian, Fedora, Arch)
  • Terminal access (Ctrl+Alt+T on most desktops)
  • Git installed (check with git --version; if missing, install via your package manager: sudo apt install git on Debian/Ubuntu, sudo dnf install git on Fedora)
  • Optional: a GitHub, GitLab, or Bitbucket account for remote repositories

Step-by-Step

  1. Configure your identity – Git records who made each change. Set your name and email globally (or per repository with --local):
    git config --global user.name "Your Name"
    git config --global user.email "you@example.com"
  2. Navigate to your project – Use cd to enter the directory you want to track. For example: cd ~/projects/mywebsite
  3. Initialize a repository – Run git init. This creates a hidden .git folder that stores all version history.
  4. Check the status – git status shows which files are untracked, modified, or staged. Run it after every major step to stay oriented.
  5. Stage files – Add files to the staging area with git add filename or git add . to add all changes in the current directory. Staging tells Git which changes you want to include in the next commit.
  6. Commit changes – Create a snapshot with a message: git commit -m "Initial commit". Keep messages short and descriptive (e.g., “Fix login bug”).
  7. View history – git log --oneline shows a compact list of commits. Use git log for full details.
  8. Create a branch – Branches let you work on features without affecting the main code. Create and switch to a new branch: git branch feature-login then git checkout feature-login, or use the shortcut git checkout -b feature-login.
  9. Make changes and commit – Edit files, stage them (git add .), and commit (git commit -m "Add login form"). Repeat as needed.
  10. Merge branches – When your feature is done, switch back to the main branch (git checkout main or master) and merge: git merge feature-login. Resolve any conflicts (see Troubleshooting).
  11. Push to a remote – If you have a remote repository (e.g., on GitHub), add it: git remote add origin https://github.com/username/repo.git. Then push your branch: git push -u origin main.
  12. Pull changes – To get updates from a remote: git pull. This fetches and merges in one step.

Common Mistakes

  • Committing without staging – git commit -m "msg" only commits staged changes. If you haven’t run git add, your changes aren’t included.
  • Using git add . blindly – This stages everything, including temporary files or secrets. Use git status first, and add a .gitignore file to exclude unwanted files (e.g., node_modules/, *.log).
  • Confusing git pull and git fetch – git fetch only downloads changes; git pull fetches and merges. If you have local uncommitted changes, pull may fail.
  • Deleting branches incorrectly – To delete a local branch, use git branch -d branch-name (lowercase -d only deletes if merged; -D forces).
  • Ignoring merge conflicts – When conflicts occur, Git marks files with <<<<<<< and >>>>>>>. You must manually edit the file to resolve, then stage and commit.

Tips

  • Use .gitignore – Create a file named .gitignore in your repo root and list patterns for files you never want to track (e.g., *.tmp, build/).
  • Write meaningful commit messages – Use the imperative mood: “Add feature X”, “Fix bug Y”. This makes history easier to read.
  • Commit often – Small, frequent commits are easier to revert than one giant commit.
  • Use git diff – Before staging, run git diff to see exactly what changed. After staging, use git diff --cached.
  • Learn git stash – If you need to switch branches but have unfinished changes, run git stash to save them, then git stash pop to restore later.
  • Set up SSH keys – For remote repos, SSH keys avoid typing passwords. Generate with ssh-keygen and add to your Git host.

Troubleshooting

  • “Please tell me who you are” error – You haven’t set your name/email. Run the config commands from Step 1.
  • Merge conflict – Git will list conflicting files. Open each, find the markers (<<<<<<<, =======, >>>>>>>), edit to keep the correct lines, remove the markers, then git add and git commit.
  • Accidentally committed a sensitive file – Use git rm --cached filename to stop tracking it, add it to .gitignore, and commit. Note that the file remains in history; use git filter-branch or tools like BFG if you need to purge it.
  • Undo a commit (keep changes) – git reset --soft HEAD~1 undoes the last commit but keeps your changes staged. git reset HEAD~1 unstages them but keeps working tree.
  • Undo a commit (discard changes) – git reset --hard HEAD~1 permanently removes the last commit and its changes. Use with extreme caution.
  • “Not a git repository” error – You’re not in a directory with a .git folder. Run git init or navigate to the correct directory.
  • Push rejected (non-fast-forward) – Someone else pushed changes. Run git pull first, resolve conflicts, then push again.

Frequently Asked Questions

How do I undo the last commit but keep my changes?

Run git reset --soft HEAD~1. This moves the HEAD pointer back one commit, leaving your changes staged. You can then re-commit or edit.

What’s the difference between git fetch and git pull?

git fetch downloads new data from the remote but doesn’t integrate it into your working files. git pull does fetch and then merges the changes into your current branch.

How do I ignore files I don’t want to commit?

Create a file named .gitignore in your repository root. Each line is a pattern (e.g., *.log, temp/). Then stage and commit the .gitignore file itself.

What is a branch and why should I use one?

A branch is a separate line of development. It lets you work on a new feature or fix without affecting the stable main branch. Once tested, you can merge it back.

How do I rename a branch?

If you’re on the branch, use git branch -m new-name. If you’re on another branch, use git branch -m old-name new-name.

Can I recover a deleted branch?

If you know the commit hash, you can recreate it: git branch branch-name commit-hash. If you don’t have the hash, check git reflog to find the last commit of that branch.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →