Git Push Cheatsheet

Basic Push:

This command pushes changes from your local branch to the corresponding remote branch.

git push

Push Specific Branch:

Replace <remote_name> with the name of the remote repository (usually origin) and <branch_name> with the name of the branch you want to push.

git push <remote_name> <branch_name>

Push All Branches:

Pushes all branches to the remote repository.

git push --all

Push Tags:

Pushes all tags to the remote repository.

git push --tags

Force Push:

Forces the push operation, overwriting remote changes with your local changes. Use with caution.

git push --force

Push a New Branch:

git push -u <remote_name> <branch_name>

Pushes the specified branch to the remote repository and sets it to track the upstream branch.

Push with Force and Lease:

A safer alternative to –force, which checks if the remote ref is updated. Helps prevent unintentional overwriting of changes.

git push --force-with-lease

Push a Commit to Different Branch:

Pushes a specific commit to the specified branch on the remote repository.

git push <remote_name> <commit_hash>:<branch_name>

Push to Multiple Repositories:

Adds multiple push URLs for a remote repository. Helpful for pushing to multiple remotes simultaneously.

git remote set-url --add --push <remote_name> <url>

Pushing to Multiple Repositories

Suppose you have a repository hosted on both GitHub and GitLab, and you want to push changes to both:

git remote set-url --add --push origin
git remote set-url --add --push origin
git push origin master

This configures your origin remote to have two push URLs, one for GitHub and one for GitLab. Then, when you push changes to origin master, Git pushes to both repositories.

Remember to replace placeholders like <remote_name>, <branch_name>, and <url> with actual names and values relevant to your Git repository setup. Always ensure you understand the consequences of your push actions, especially when using force options.

Pushing all branches

If you want to push all branches to each repository, you can use the --all flag:

git push --all repo_name
This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.