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 giton Debian/Ubuntu,sudo dnf install giton Fedora) - Optional: a GitHub, GitLab, or Bitbucket account for remote repositories
Step-by-Step
- 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" - Navigate to your project – Use
cdto enter the directory you want to track. For example:cd ~/projects/mywebsite - Initialize a repository – Run
git init. This creates a hidden.gitfolder that stores all version history. - Check the status –
git statusshows which files are untracked, modified, or staged. Run it after every major step to stay oriented. - Stage files – Add files to the staging area with
git add filenameorgit add .to add all changes in the current directory. Staging tells Git which changes you want to include in the next commit. - Commit changes – Create a snapshot with a message:
git commit -m "Initial commit". Keep messages short and descriptive (e.g., “Fix login bug”). - View history –
git log --onelineshows a compact list of commits. Usegit logfor full details. - Create a branch – Branches let you work on features without affecting the main code. Create and switch to a new branch:
git branch feature-loginthengit checkout feature-login, or use the shortcutgit checkout -b feature-login. - Make changes and commit – Edit files, stage them (
git add .), and commit (git commit -m "Add login form"). Repeat as needed. - Merge branches – When your feature is done, switch back to the main branch (
git checkout mainormaster) and merge:git merge feature-login. Resolve any conflicts (see Troubleshooting). - 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. - 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 rungit add, your changes aren’t included. - Using
git add .blindly – This stages everything, including temporary files or secrets. Usegit statusfirst, and add a.gitignorefile to exclude unwanted files (e.g.,node_modules/,*.log). - Confusing
git pullandgit fetch–git fetchonly downloads changes;git pullfetches 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.gitignorein 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, rungit diffto see exactly what changed. After staging, usegit diff --cached. - Learn
git stash– If you need to switch branches but have unfinished changes, rungit stashto save them, thengit stash popto restore later. - Set up SSH keys – For remote repos, SSH keys avoid typing passwords. Generate with
ssh-keygenand 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 addandgit commit. - Accidentally committed a sensitive file – Use
git rm --cached filenameto stop tracking it, add it to.gitignore, and commit. Note that the file remains in history; usegit filter-branchor tools like BFG if you need to purge it. - Undo a commit (keep changes) –
git reset --soft HEAD~1undoes the last commit but keeps your changes staged.git reset HEAD~1unstages them but keeps working tree. - Undo a commit (discard changes) –
git reset --hard HEAD~1permanently removes the last commit and its changes. Use with extreme caution. - “Not a git repository” error – You’re not in a directory with a
.gitfolder. Rungit initor navigate to the correct directory. - Push rejected (non-fast-forward) – Someone else pushed changes. Run
git pullfirst, 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
- Disable Autocorrect on iPhone or iPad: Quick Guide
- View Notification Count on iPhone: Full Guide
- Custom Keyboard Shortcuts on Windows 11 & macOS
- Restore Windows 11 Display Scaling to Recommended
- Semiconductor Cycle: How Investors Track It0.0
- Microsoft Store Error 0x80073d12: Fixes That Work