How To Push To Github

     

The git push command is used lớn upload local repository nội dung to lớn a remote repository. Pushing is how you transfer commits from your local repository to a remote repo. It"s the counterpart to git fetch, but whereas fetching imports commits to local branches, pushing exports commits to remote branches. Remote branches are configured using the git remote command. Pushing has the potential lớn overwrite changes, caution should be taken when pushing. These issues are discussed below.

Bạn đang xem: How To Push To Github

Git push usage


Push the specified branch lớn , along with all of the necessary commits & internal objects. This creates a local branch in the destination repository. To prsự kiện you from overwriting commits, Git won’t let you push when it results in a non-fast-forward merge in the destination repository.


Same as the above comm&, but force the push even if it results in a non-fast-forward merge. Do not use the --force flag unless you’re absolutely sure you know what you’re doing.


Tags are not automatically pushed when you push a branch or use the --all option. The --tags flag sends all of your local tags lớn the remote repository.

Git push discussion

git push is most commonly used lớn publish an upload local changes to a central repository. After a local repository has been modified a push is executed lớn tóm tắt the modifications with remote team members.

*

The above sầu diagram shows what happens when your local master has progressed past the central repository’s master và you publish changes by running git push origin master. Notice how git push is essentially the same as running git merge master from inside the remote repository.

Git push & syncing

git push is one component of many used in the overall Git "syncing" process. The syncing commands operate on remote branches which are configured using the git remote command. git push can be considered & "upload" command whereas, git fetch và git pull can be thought of as "download" commands. Once changesets have been moved via a download or upload a git merge may be performed at the destination to lớn integrate the changes.

Pushing khổng lồ bare repositories

A frequently used, modern Git practice is to have sầu a remotely hosted --bare repository act as a central origin repository. This origin repository is often hosted off-site with a trusted 3rd tiệc nhỏ lượt thích Bitbucket. Since pushing messes with the remote branch structure, It is safest và most comtháng khổng lồ push khổng lồ repositories that have been created with the --bare flag. Bare repos don’t have sầu a working directory so a push will not alter any in progress working directory content. For more information on bare repository creation, read about git init.

Xem thêm: Quảng Cáo Google Adwords Hiệu Quả, 7 Thủ Thuật Chạy Google Ads Bạn Cần Phải Biết

Force Pushing

Git prevents you from overwriting the central repository’s history by refusing push requests when they result in a non-fast-forward merge. So, if the remote history has diverged from your history, you need khổng lồ pull the remote branch và merge it into your local one, then try pushing again. This is similar lớn how Stoàn quốc makes you synchronize with the central repository via svn update before committing a changemix.

The --force flag overrides this behavior & makes the remote repository’s branch match your local one, deleting any upstream changes that may have sầu occurred since you last pulled. The only time you should ever need to lớn force push is when you realize that the commits you just shared were not quite right and you fixed them with a git commit --amend or an interactive rebase. However, you must be absolutely certain that none of your teammates have pulled those commits before using the --force option.

Examples

Default git push

The following example describes one of the standard methods for publishing local contributions khổng lồ the central repository. First, it makes sure your local master is up-to-date by fetching the central repository’s copy & rebasing your changes on top of them. The interactive rebase is also a good opportunity lớn clean up your commits before sharing them. Then, the git push commvà sends all of the commits on your local master to the central repository.


gitcheckoutmastergitfetchoriginmastergitrebase-iorigin/master#Squashcommits,fixupcommitmessagesetc.gitpushoriginmaster
Since we already made sure the local master was up-to-date, this should result in a fast-forward merge, and git push should not complain about any of the non-fast-forward issues discussed above.

Amended force push

The git commit command accepts a --amover option which will update the previous commit. A commit is often amended to lớn update the commit message or add new changes. Once a commit is amended a git push will fail because Git will see the amended commit & the remote commit as diverged content. The --force option must be used khổng lồ push an amended commit.


#makechangestoarepoandgitaddgitcommit--amend#updatetheexistingcommitmessagegitpush--forceoriginmaster
The above example assumes it is being executed on an existing repository with a commit history. git commit --amover is used to lớn update the previous commit. The amended commit is then force pushed using the --force option.

Deleting a remote branch or tag

Sometimes branches need khổng lồ be cleaned up for book keeping or organizational purposes. The fully delete a branch, it must be deleted locally & also remotely.


The above sầu will delete the remote branch named branch_name passing a branch name prefixed with a colon to lớn git push will delete the remote branch.