Lesson 33 of 60 – Git Push
55%

Git Push

The git push command is used to send local commits from your Git repository to a remote repository such as GitHub. It is one of the most commonly used commands when working with remote repositories.

Note: Before using git push, make sure your local repository has a configured remote and that your changes have been committed.

1. What is Git Push?

git push transfers local commits to a remote repository.

Local Repository
       ↓
   git push
       ↓
Remote Repository

For example, you can push your local commits to GitHub.

2. Why Use Git Push?

Git push is used when you want to send your local commits to a remote repository.

  • Upload committed changes
  • Share code with team members
  • Update a GitHub repository
  • Publish a branch
  • Back up project history remotely

3. Basic Git Push Syntax

The basic syntax is:

git push <remote> <branch>

Example:

git push origin main

Here origin is the remote name and main is the branch name.

4. What is origin?

origin is a commonly used name for the primary remote repository.

For example:

git push origin main

This means push the main branch to the remote named origin.

5. What is the Branch?

The branch specifies which local branch should be pushed.

git push origin main

git push origin feature-login

The first command pushes main, while the second pushes the feature-login branch.

6. Check the Current Branch

Before pushing, you can check your current branch:

git branch

The current branch is normally marked with an asterisk.

* main
  feature-login

7. Check Repository Status

Use git status before pushing to understand the state of your working directory and staging area.

git status

Remember that git push sends commits, not simply every modified file in your working directory.

8. Commit Before Push

A common workflow is to stage and commit your changes before pushing:

git add .
git commit -m "Update project"
git push origin main

The push sends the committed changes to the remote repository.

9. First Push

For the first push of a local branch, you can use:

git push -u origin main

The -u option sets the upstream tracking relationship between the local branch and the remote branch.

10. What Does -u Mean?

The -u option is short for --set-upstream.

git push -u origin main

It records the remote tracking relationship so later commands can often use:

git push

11. Simple Future Push

After upstream tracking has been configured, you can usually use:

git push

Git uses the configured upstream branch for the push.

12. Push a Feature Branch

Suppose you have a branch called feature-login. You can push it with:

git push -u origin feature-login

This publishes the branch to the remote and sets its upstream relationship.

13. Push a New Branch

A complete example is:

git switch -c feature-dashboard

git add .
git commit -m "Add dashboard"

git push -u origin feature-dashboard

The new branch is pushed to the remote repository.

14. Push Multiple Branches

Each branch can be pushed separately.

git push origin main
git push origin develop
git push origin feature-login

This allows different branches to be published to the remote repository.

15. Push All Branches

Git can push all local branches using:

git push --all origin

Use this carefully because it can publish multiple local branches to the remote.

16. Push Tags

A specific tag can be pushed using:

git push origin v1.0.0

You can also push all local tags:

git push origin --tags

17. Push to a Different Remote

A repository can have multiple remotes. You can specify which remote should receive your branch.

git push upstream main

Here upstream is the remote name.

18. Check Remote Before Pushing

Before pushing, it can be useful to check the configured remote:

git remote -v

Example:

origin  https://github.com/username/project.git (fetch)
origin  https://github.com/username/project.git (push)

19. Push Rejected

Sometimes a push is rejected because the remote branch contains changes that are not present in your local branch.

A common next step is to retrieve the remote changes and integrate them appropriately before pushing again.

git pull

The exact resolution depends on the repository history and the reason for the rejection.

20. Non-Fast-Forward Push

A non-fast-forward rejection can happen when the remote branch has commits that your local branch does not contain.

One common approach is:

git pull --rebase

git push

Review the changes and resolve any conflicts if necessary.

21. Force Push

Git provides force-push options for cases where you intentionally need to replace the remote branch history.

git push --force

Force pushing can overwrite remote history, so it should be used carefully, especially on shared branches.

22. Safer Force Push with --force-with-lease

A safer alternative in many workflows is:

git push --force-with-lease

It checks whether the remote reference is still at the expected state before replacing it.

Even this option should be used carefully on shared branches.

23. Push and Set Upstream

For a new branch, the commonly used command is:

git push -u origin feature-login

After this, you can usually use:

git push

git pull

24. Push Workflow

# Check changes
git status

# Stage changes
git add .

# Commit changes
git commit -m "Update login page"

# Push commit
git push

This is a common daily workflow after upstream tracking has already been configured.

25. Authentication During Push

When pushing to GitHub, Git must authenticate the operation according to your configured remote and authentication method.

Depending on the setup, this may involve HTTPS credentials, credential management, or SSH keys.

Important: Never place passwords, personal access tokens, or private SSH keys in your project files or commit them to Git.

26. Common Git Push Mistakes

  • Trying to push before creating a commit.
  • Using the wrong remote name.
  • Pushing the wrong branch.
  • Using the wrong repository URL.
  • Ignoring a push rejection.
  • Using force push without understanding its effect.
  • Sharing authentication credentials.
  • Accidentally pushing sensitive files.

27. Checking the Upstream Branch

You can inspect branch tracking information with:

git branch -vv

Example:

* main  1a2b3c4 [origin/main] Update project

This indicates that the local main branch is tracking origin/main.

28. Complete Git Push Example

git status

git add .

git commit -m "Add student dashboard"

git remote -v

git push -u origin main

After the first upstream push, future changes can normally use:

git add .
git commit -m "Update dashboard"
git push

29. Git Push Checklist

  • Check your current branch.
  • Check the repository status.
  • Review the files being committed.
  • Create a meaningful commit.
  • Verify the remote if necessary.
  • Push the correct branch.
  • Check the result on the remote repository.

30. Summary of Git Push

The git push command sends local commits to a remote repository. It is an essential command for publishing local changes to GitHub and collaborating with other developers.

git add .
git commit -m "Update project"
git push

For the first push of a branch, you can use:

git push -u origin main

📌 Key Points

  • git push sends local commits to a remote repository.
  • The basic syntax is git push <remote> <branch>.
  • origin is a common remote name.
  • git push origin main pushes the main branch to origin.
  • git push -u origin main sets upstream tracking.
  • After upstream tracking is configured, git push can usually be used.
  • A new feature branch can be published with git push -u origin branch-name.
  • Tags can be pushed using git push origin tag-name.
  • git push --all origin can push all local branches.
  • Force pushing can overwrite remote history and should be used carefully.
  • --force-with-lease provides an additional safety check for force pushes.
  • Always verify the correct remote and branch before pushing.

🧠 Quick Quiz

Question: Which command pushes the local main branch to the remote named origin and sets the upstream branch?