How to Push a Change to Git in Linux
In this article
To push a change to Git in Linux, you stage your changes with git add, commit them with git commit, and then run git push to upload them to the remote repository. This guide walks through the exact commands, common pitfalls, and how to fix push errors.
What You'll Need
- A Linux terminal (any distribution works).
- Git installed (
sudo apt install giton Debian/Ubuntu,sudo dnf install giton Fedora). - An existing Git repository (local) with at least one commit already made.
- A remote repository (like GitHub, GitLab, or a self-hosted server) that you have write access to.
- Authentication set up (SSH key or HTTPS credentials) – if you haven't done this, see the Troubleshooting section below.
Step-by-Step
- Open a terminal and navigate to your repository folder:
cd /path/to/your/repo. - Check the current status to see what files have changed:
git status. This shows modified, untracked, and staged files. - Review the actual changes before staging. For modified files, use
git diffto see line-by-line differences. For untracked files, open them with a text editor. - Stage the changes you want to commit. Use
git add filenamefor specific files, orgit add .to stage all changes (including deletions). To stage only tracked modifications, usegit add -u. - Verify what is staged:
git statusagain – the staged files will be under “Changes to be committed”. - Commit the staged changes with a clear message:
git commit -m "Describe your change". If you need a multi-line message, omit the-mand use the default editor. - Check your remote and branch:
git remote -vshows the remote URL, andgit branchshows your current branch (usuallymainormaster). - Pull any remote changes (if others work on the same branch) to avoid conflicts:
git pull origin main(replacemainwith your branch name). If you have local commits and the remote has new commits, Git will merge them automatically unless there are conflicts. - Push your commit to the remote:
git push origin main. Replaceoriginwith your remote’s name (usuallyorigin) andmainwith your branch name. - Confirm success: Git prints something like “To github.com:user/repo.git” and “main -> main”. You can also run
git log --oneline -1to see your latest commit.
Common Mistakes
- Forgetting to stage files – if you commit without
git add, your changes aren’t included. Always checkgit statusbefore committing. - Committing to the wrong branch – make sure you’re on the intended branch with
git branchbefore committing. - Pushing without pulling first – if the remote has commits you don’t have, your push will be rejected. Always pull before pushing.
- Using
git pushwithout specifying remote/branch – this works only if your branch has an upstream set. Otherwise, usegit push -u origin mainthe first time. - Accidentally staging large or sensitive files – use
.gitignoreto exclude them, and never commit passwords or API keys.
Tips
- Write meaningful commit messages – use imperative mood (“Fix login bug” not “fixed login bug”).
- Commit often – small, logical commits are easier to revert and review.
- Use
git diff --stagedbefore committing to review exactly what will be committed. - Set up an upstream branch the first time you push a new branch:
git push -u origin branch-name. This lets you usegit pushandgit pullwithout extra arguments. - Use SSH keys for authentication – they’re more secure than passwords and you won’t need to type credentials each time.
- If you make a mistake in the last commit, use
git commit --amendto edit the message or add forgotten files (only if you haven’t pushed yet).
Troubleshooting
Push rejected (non-fast-forward) – This means the remote has commits you don’t have locally. Run git pull origin main to merge them, then push again. If you get merge conflicts, resolve them in your editor, then git add the resolved files and git commit.
Authentication failed – For HTTPS, Git may ask for a username/password. If you have two-factor authentication enabled, you need a personal access token instead of your password. For SSH, ensure your public key is added to your Git hosting account. Test with ssh -T git@github.com (for GitHub) to see if the key works.
“fatal: not a git repository” – You’re not in the correct directory. Use cd to navigate to your repo, or run git init if you haven’t initialized one.
“src refspec main does not match any” – This means the branch main doesn’t exist locally. Check your branch with git branch and use the correct name (e.g., master).
“Permission denied (publickey)” – Your SSH key isn’t set up. Generate one with ssh-keygen, add the public key to your Git host, and ensure your remote URL uses SSH (git@github.com:user/repo.git) not HTTPS.
Push hangs or times out – Check your internet connection. If you’re behind a firewall, you may need to use HTTPS instead of SSH, or configure a proxy.
Frequently Asked Questions
How do I push a new branch for the first time?
Use git push -u origin branch-name. The -u flag sets the upstream, so subsequent pushes can be just git push.
What if I accidentally pushed a commit with sensitive data?
Immediately remove the file from the repository and push a new commit. However, the data remains in Git history. You need to rewrite history with git filter-branch or use tools like BFG Repo-Cleaner, and then force-push. This is complex and should be done carefully – if the repo is public, consider the data compromised.
Can I undo a push?
You can revert the commit with git revert and push the revert. Or you can reset locally and force-push (git push --force), but this is dangerous if others have pulled the changes – use it only on your own branch.
Why does git push ask for a password every time?
You’re using HTTPS. To avoid this, either use SSH keys or configure a credential helper: git config --global credential.helper cache (caches temporarily) or store (saves permanently, but less secure).
What is the difference between git push and git commit?
git commit saves your changes locally in your repository. git push uploads those local commits to the remote server. You need both to share your work.
How do I push to a different remote than origin?
Add another remote with git remote add name url, then push with git push name branch. For example, git remote add upstream https://github.com/other/repo.git then git push upstream main.