# Pushing Changes

> How to upload your local commits to a remote repository to share them with your team.

Subject: Version Control | Lesson 15 of 29 | 4 min read
Interactive lesson: https://codewithanimation.in/git/git-push
Previous: [Cloning a Repository](https://codewithanimation.in/git/git-clone.md) | Next: [Fetching & Pulling](https://codewithanimation.in/git/git-fetch-and-pull.md)

## Pushing Changes

`git push` updates one or more branches, tags, or other references in a remote repository from your local repository.

It sends all necessary data (commits and objects) that isn't already on the remote, bringing the remote branch up-to-date with your local progress.

## Why it matters

Web developers use `git push` to **share code**.

While `git commit` saves changes locally, `git push` uploads those changes to a shared remote repository (like GitHub or GitLab). This makes your features, bug fixes, or UI updates available to the rest of the team for review, collaboration, and deployment.

## How it works

When executing a command like `git push origin main`, Git calculates which commits exist on your local `main` branch but are missing from the remote `main` branch.

It then pushes only those missing local commits over the network to the remote repository, updating the remote branch's reference to match your local branch's latest commit.

## Common mistake: Non-Fast-Forward Rejections

The #1 common mistake is having your push rejected because the remote has changes you don't have locally (a 'non-fast-forward' rejection).

This usually happens when a teammate pushes their work before you do. In a panic, beginners often resort to `git push --force` (`-f`), which forcibly overwrites the remote branch history and permanently deletes the teammate's newly pushed code.

## Tip: Always pull before you push

Always pull before you push. Running `git pull` fetches and integrates your team's latest remote changes into your local branch.

Once your local history includes their work and any merge conflicts are resolved, your `git push` will succeed safely without risking data loss.

## Try it: Push a branch safely

Compare a fast-forward push with a remote that has advanced since the last fetch.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/git/git-push

## Check yourself

### Question 1

What does `git push` actually do behind the scenes?

- A. It downloads the latest code from the remote repository to your local machine.
- B. It permanently deletes your local branch after sending it to the remote.
- C. It updates remote references and sends associated objects (commits) that aren't on the remote yet.
- D. It creates a brand new repository on GitHub and links it to your current folder.

Correct answer: C

According to the Git documentation, `git push` updates remote references (like branches) and sends the necessary data (objects/commits) from your local repository to the remote.

- Why not A: That is `git pull`.
- Why not B: Your local branch remains exactly where it is.
- Why not D: You must create the repo on GitHub manually (or via CLI tool) first.

### Question 2

You try to run `git push`, but Git rejects it with a 'non-fast-forward' error. What is the most likely cause?

- A. You don't have an active internet connection.
- B. Your local repository is empty and has no commits to push.
- C. Someone else has pushed changes to the remote branch, and your local branch is behind.
- D. You used the wrong password for your remote repository account.

Correct answer: C

A 'non-fast-forward' error occurs when the remote repository contains commits that your local repository doesn't have yet. You must integrate those remote changes (via `git pull`) before you can push your own.

- Why not A: Git would give a connection or resolution error.
- Why not B: Git would just say everything is up to date.
- Why not D: Git would give an Authentication Failed error.

### Question 3

Why is `git push --force` considered dangerous in a team environment?

- A. It deletes the local repository on all of your teammates' computers.
- B. It can overwrite the remote branch history, potentially erasing work pushed by other developers.
- C. It automatically accepts all merge conflicts without letting you review them.
- D. It forces Git to switch to a different branch before pushing.

Correct answer: B

Forcing a push overrides the remote branch to exactly match your local branch. If teammates have pushed commits that you haven't pulled down, those commits will be lost from the remote history.

- Why not A: Git commands don't magically delete things on other people's computers.
- Why not C: Pushing doesn't merge conflicts; pulling does.
- Why not D: It just forces the push on the current branch.

## Remember this

- `git push` uploads your local commits to a remote.
- If the remote has commits you don't have, your push will be rejected.
- Always pull before you push to resolve conflicts locally first.
