Course Tonight

Course Tonight

Did You Know?

You can create any type of product documentation with Docy

Chapter 29: Pushing

Estimated reading: 5 minutes 4 views
Parameter Details
–force Overwrites the remote ref to match your local ref. Can cause the remote repository to lose commits, so use with care.
–verbose Run verbosely.
<remote> The remote repository that is the destination of the push operation.
<refspec>… Specify what remote ref to update with what local ref or object.

After changing, staging, and committing code with Git, pushing is required to make your changes available to others and transfer your local changes to the repository server. This topic will cover how to properly push code using Git.

Section 29.1: Push a specific object to a remote branch

General syntax:

git push <remotename> <object>:<remotebranchname>

Example:

git push origin master:wip-yourname

This will push your master branch to the wip-yourname branch of the origin repository (usually the repository you cloned from).

Delete remote branch:

Deleting the remote branch is equivalent to pushing an empty object to it.

git push <remotename> :<remotebranchname>

Example:

git push origin :wip-yourname

This will delete the remote branch wip-yourname. Instead of using the colon, you can also use the --delete flag, which is more readable in some cases.

Example:

git push origin --delete wip-yourname

Push a single commit:

If you have a single commit in your branch that you want to push to a remote without pushing anything else, you can use the following command:

git push <remotename> <commit SHA>:<remotebranchname>

Example: Assuming a git history like this:

eeb32bc Commit 1 - already pushed
347d700 Commit 2 - want to push
e539af8 Commit 3 - only local
5d339db Commit 4 - only local

To push only commit 347d700 to the remote master, use the following command:

git push origin 347d700:master

Section 29.2: Push

To push your code to your existing upstream, use the command:

git push

Depending on the push configuration, it will either push code from your current branch (default in Git 2.x) or from all branches (default in Git 1.x).

Specify remote repository

When working with Git and having multiple remote repositories, you can specify a remote repository to push to by appending its name to the command:

git push origin

Specify Branch

To push to a specific branch, such as feature_x, use the command:

git push origin feature_x

Set the remote tracking branch

If the branch you are working on doesn’t come from a remote repository, you need to perform the following command to tell Git to push the current branch to a specific remote/branch combination:

git push --set-upstream origin master

Here, master is the branch name on the remote origin. You can use -u as a shorthand for --set-upstream.

Pushing to a new repository

To push to a repository that you haven’t created yet or is empty, follow these steps:

  1. Create the repository on GitHub (if applicable).
  2. Copy the URL given to you in the form https://github.com/USERNAME/REPO_NAME.git.
  3. Go to your local repository and execute git remote add origin URL to add the remote repository.
  4. Verify it was added by running git remote -v.
  5. Run git push origin master to push your code to the new repository.

Your code should now be on GitHub. For more information, view “Adding a remote repository.”

Explanation

Pushing code means that Git will analyze the differences between your local commits and the remote repository and send them to be written on the upstream. When the push succeeds, your local and remote repositories are synchronized, and other users can see your commits.

Section 29.3: Force Pushing

Sometimes, when you have local changes that are incompatible with remote changes, the only way to push your changes is a force push. Use the following commands:

git push -f

or

git push --force

Important notes:

  • This will overwrite any remote changes, and your remote will match your local.
  • Using this command may cause the remote repository to lose commits. It is strongly advised against doing a force push if you are sharing this remote repository with others, as it can render their work out of sync with the remote repository.
  • Only force push when nobody except you has pulled the changes you are trying to overwrite or when you can force everyone to clone a fresh copy after the forced push and make everyone apply their changes to it.

Section 29.4: Push tags

To push all the Git tags in the local repository that are not in the remote repository, use the command:

git push --tags

This will push all the Git tags to the remote repository.

Section 29.5: Changing the default push behavior

By default, Git has different push behaviors. You can change the default push behavior using the following commands:

  • current: Updates the branch on the remote repository that shares a name with the current working branch.
git config push.default current
  • simple: Pushes to the upstream branch but will not work if the upstream branch is called something else.
git config push.default simple
  • upstream: Pushes to the upstream branch, regardless of its name.
git config push.default upstream
  • matching: Pushes all branches that match on the local and remote repositories.
git config push.default matching

After you’ve set the preferred push behavior, you can use the command git push to update the remote repository according to the chosen behavior.

Leave a Comment

Share this Doc

Chapter 29: Pushing

Or copy link

CONTENTS