How to Pull a Git Repository in Linux
In this article
To pull a Git repository in Linux, open a terminal and run git pull inside the repository’s directory. This fetches the latest changes from the remote and merges them into your current branch. If you haven’t cloned the repository yet, first run git clone <repository-url>. This guide covers the exact commands, common pitfalls, and fixes.
What You'll Need
- A Linux system with Git installed (check with
git --version; if missing, install via your package manager, e.g.,sudo apt install giton Debian/Ubuntu). - The URL of the remote repository (HTTPS or SSH).
- Authentication credentials if the repository is private (username/password or SSH key).
Step-by-Step
- Open a terminal. Use your distribution’s terminal emulator (e.g., GNOME Terminal, Konsole) or a virtual console (Ctrl+Alt+F3).
- Check if Git is installed. Run
git --version. If you see a version number, proceed. If not, install Git using your package manager (e.g.,sudo apt install gitfor Ubuntu/Debian,sudo dnf install gitfor Fedora). - Navigate to the repository directory. Use
cd /path/to/your/repo. If you haven’t cloned the repo yet, skip to step 4; otherwise, go to step 5. - Clone the repository (if not already done). Run
git clone <repository-url>. For example:git clone https://github.com/username/repo.git. This creates a new directory with the repo’s name. Thencdinto it. - Check your current branch. Run
git branchto see which branch you’re on. The current branch is marked with an asterisk (*). - Pull the latest changes. Run
git pull. This fetches changes from the remote branch that tracks your current branch and merges them into your working copy. - Handle merge conflicts (if any). If Git reports a conflict, open the affected files, look for conflict markers (
<<<<<<<,=======,>>>>>>>), edit to keep the correct content, then rungit add <file>andgit committo finalize the merge. - Verify the update. Run
git log --oneline -5to see recent commits, orgit statusto confirm your working tree is clean.
Common Mistakes
- Forgetting to commit or stash local changes. If you have uncommitted changes,
git pullwill fail with an error like “Your local changes would be overwritten by merge.” Commit your changes (git commit -am "message") or stash them (git stash) before pulling. - Pulling the wrong branch. Ensure you’re on the branch you want to update. Use
git checkout mainorgit switch mainto switch. - Using the wrong remote name. The default remote is usually
origin. If you have multiple remotes, specify it:git pull origin main. - Typing the URL incorrectly when cloning. A single typo can cause a “repository not found” error. Copy the URL from the hosting service (GitHub, GitLab, etc.).
- Ignoring merge conflicts. Do not force-pull with
git pull --force; it can overwrite your local commits. Resolve conflicts properly.
Tips
- Use
git pull --rebaseto apply your local commits on top of the remote changes, keeping a linear history. This is useful if you have local commits and want to avoid merge commits. - Use
git fetchfollowed bygit mergeif you want to inspect changes before merging.git fetchdownloads changes but doesn’t merge them. - Set up SSH keys for passwordless pulls. Generate a key with
ssh-keygen, add the public key to your hosting service, and use SSH URLs (e.g.,git@github.com:user/repo.git). - Check what will be pulled before pulling:
git fetchthengit log HEAD..origin/main --onelineto see incoming commits. - If you work with large files, consider using
git pull --depth 1to fetch only the latest commit, but note this may break history-dependent operations.
Troubleshooting
If the pull does not go as planned, try these steps in order:
- Check your network connection. Run
ping github.com(or your hosting domain) to see if you have connectivity. - Verify the remote URL. Run
git remote -vto see the configured remotes. If the URL is wrong, update it withgit remote set-url origin <correct-url>. - Check authentication. For HTTPS, Git may prompt for credentials. If you’re using a token, ensure it’s valid. For SSH, test with
ssh -T git@github.com(for GitHub) to see if your key works. - Update Git. Older Git versions may have bugs. Update via your package manager.
- If you get “fatal: not a git repository,” you’re not in the right directory. Use
pwdto check, thencdto the repo. - If you get “error: Your local changes would be overwritten,” commit or stash your changes as mentioned above.
- If merge conflicts occur and you’re unsure how to resolve them, abort the merge with
git merge --abortto return to the pre-pull state, then seek help or resolve carefully.
Frequently Asked Questions
What is the difference between git pull and git fetch?
git fetch downloads the latest changes from the remote but does not merge them into your working branch. git pull does both: fetch and merge (or rebase if you specify --rebase).
Can I pull a specific branch?
Yes, run git pull origin branch-name. This fetches that branch and merges it into your current branch. To switch to that branch first, use git checkout branch-name or git switch branch-name.
What does “Already up to date” mean?
It means your local branch already has all the commits from the remote branch you’re pulling from. No changes were needed.
How do I pull without committing my local changes?
Stash your changes with git stash, pull, then reapply them with git stash pop. Be aware that if conflicts arise during pop, you’ll need to resolve them manually.
Why do I get “Permission denied (publickey)” when pulling via SSH?
Your SSH key is either not set up or not added to your hosting account. Generate a key with ssh-keygen, add the public key to your Git hosting profile, and ensure the SSH agent is running (eval $(ssh-agent) and ssh-add).
Is it safe to force pull?
No, git pull --force is not a standard option (it can be used with --force-with-lease in some contexts, but it’s risky). It can overwrite local commits. Instead, reset your branch to match the remote with git reset --hard origin/main, but be aware this discards all local commits and uncommitted changes permanently.