Distributed Workflows


Remote branches are references to the state of branches on your remote repositories. They’re local branches that you can’t move; they’re moved automatically whenever you do any network communication.
If you clone a Git repository from the remote Git server, git automatically names it origin. Git also gives you your own master branch starting at the same place as origin’s master branch. Remote branches take the form [remote-server]/[branch]. For instance, if you wanted to see what the master branch on your origin remote looked like as of the last time you communicated with it, you would check the origin/master branch.
If you do some works on your local master branch,  and, someone else pushes to the remote server's master branch, your origin/master branch is out of sync with the remote server's master branch.

To synchronize your work, you run a git fetch origin command. This command looks
up which server origin is, fetches any data from it that you don’t yet have, and updates your local database, moving your origin/master pointer to its new, more up-to-date position.
You can add a remote repository by defining it:
git remote add  remote-server url
Because that server is derived from the same origin server, Git fetches no data but sets a branch called remote-server/master to point to the commit that  remote-server has as its master branch.
You can use git remote -v to show the aliases of the remote server.

Pushing

When you want to share a branch with the world, you need to push it up to a remote that you have write access to.

git push remote-server local-branch
git push remote-server local-branch/remote-branch

It’s important to note that when you do a fetch that brings down new remote branches, you don’t automatically have local, editable copies of them. if you want to work on a remote branch, you need to merge it into your current working branch.
git merge origin/remote-branch
If you want to work on the remote branch locally,
git checkout -b branch origin/remote-branch

Tracking Branches

Checking out a local branch from a remote branch automatically creates what is called a tracking branch. Tracking branches are local branches that have a direct relationship to a remote branch. When you work on a tracking branch, Git automatically knows which server and branch you want to push/pull.
When you clone a repository, it generally automatically creates a master branch to  track origin/master.

Deleting Remote Branches

git push remote-server :remote-branch

Rebasing

In Git, there are two main ways to integrate changes from one branch into another: the merge and the rebase.
The merge command performs a three-way merge between the two latest branch snapshots (C3 and C4) and the most recent common ancestor of the two (C2), creating a new snapshot :

With the rebase command, you can take all the changes that were committed on one branch and replay them on another one.
It works by going to the common ancestor of the two branches (the one you’re on and the one you’re rebasing onto), getting the diff introduced by each commit of the branch you’re on, saving those diffs to temporary files, resetting the current branch to the same commit as the branch you are rebasing onto, and finally applying each change in turn.


At this point, you can go back to the master branch and do a fast-forward merge.





留言

熱門文章