[Git]Github代码提交管理流程

如果公司在Github建立了私有服务器,项目进行中需要多人多地协同工作。这时需要一套完整规范的代码提交流程。分享一下我们的经验。

第一步建立中央仓库upstream,然后建立fork:

Click the Fork button on the original repository and then clone your fork on your local machine.
# Clone fork locally
git clone git@github.ibm.com:USERNAME/FORKED-PROJECT.git


第二步需要经常使得本地fork与中央仓库的upstream保持同步
Keep your fork up to date by tracking the original upstream repository that you forked and adding it as remote on your local machine.
# Add 'upstream' repo to list of remotes
git remote add upstream https://github.ibm.com/<org>/ORIGINAL-PROJECT.git# Verify the new remote named 'upstream'
git remote -v

To update your fork with the latest upstream changes, fetch the upstream repository’s branches and latest commits into your local repository.
# Fetch from upstream remote
git fetch upstream

Checkout your own master branch and rebase it from the upstream repository’s master branch to get the latest changes in upstream.
# Checkout your master branch and rebase upstream
git checkout mastergit rebase upstream/master

Git will fast-forward local master if there are no new commits on the local master branch. If you've been making changes to master you may have conflicts.

第三步建立feature branch:
New branches should be created whenever you start working on new features or bug fixes. This helps keep your changes organized and separated from the master branch and will allow you to submit separate pull requests for every feature/bug fix you complete.
After your local master branch is up to date with latest changes in upstream, create a new branch based of your local master.
# Checkout the master branch so that your new branch comes from master
git checkout master

# Create a new local branch, then switch to it
git checkout -b myNewfeature origin/master
Work on your local branch and commit your changes to it.

If commits have been made to the upstream master branch, your feature branch should be rebased to get the latest changes.
# Fetch upstream and rebase upstreamgit fetch upstreamgit rebase upstream/master
Rebasing will put the commits your team members have done below all the commits you've made on the branch.
Rebasing will not work, if you have unstaged changes. If you are not ready to stage/commit, you will need to stash your changes before running rebase.
# Stash unstaged changesgit stash
After the rebase, you can re-apply the staged changes. Please note you may need to resolve potential merge conflicts while re-applying the stashed changes.
# Reapply stashed changesgit stash pop
If you're unsure what was stashed, you can view your stashes and see what a particular stash has.
# List all stored stashesgit stash list# View diff on a specific stashgit stash show -p <stashId>

When the upstream repository contains file changes in the same file that you are working on in your branch, git is unable to automatically merge your changes. We must manually help git resolve the conflicts.
Git will tell you when there are conflicts during a rebase, but if you are unsure you can check with the command git status.
If you're using atom to develop, there is a great package called merge-conflicts that can help identify the merge conflicts and stage the files.

第四步,提交代码:
From your feature branch, push all of your changes to a remote branch on your fork.
# Push the current branch and set default remote as upstream branch
git push -u origin myNewFeature
The command above is specific for when it's the first time you are pushing the local remote to your fork.
If you rebase a local branch that you have already pushed into a remote branch...
- please read the following very carefully:
If you previously pushed the local branch into a remote branch on your fork, and have since rebased with newer changes from upstream master, you will need to replace the remote branch. With the applied changes from upstream, your local branch now has a commit that is not a descendant of it. Your copy of the branch and the server’s copy are now considered completely different.
You will need to force push the local branch to replace the remote branch.
git push -f origin myNewFeature
If you run git pull, you will end up with two copies of the branch which are merged with a merge commit. This will mess up the commit history and reintroduce all the changes that were made with the rebase.
If you need to rename a remote branch, you can run the following command:
# Rename branch when pushing to remotegit push origin localBranchName:newRemoteBranchName
On Github, go to your fork page and select the feature branch myNewFeature. From this branch view, select the new pull request button.
If you need to make changes to your pull request, just push the updates to your Github fork on the same branch. The pull request will track the changes on your branch and update itself.
After your work has been incorporated into the upstream repository, you can safely remove the branch used for the pull request.
# Delete local branch
git branch -d myNewFeature
If there are uncommitted changes that you don't need, you will have to force deletion with the -D flag:
# Force delete local branch
git branch -D myNewFeature
To remove the remote branch, prepend a colon sign before the branch name.
# Delete the remote branch
git push origin :myNewFeature
You can also delete the remote branch directly from Github's UI.
When two or more developers are working on a shared task or user story, a multi-level pull request flow helps keep the collaboration organized.
For shared tasks, the developers work on their own fork, but there's a single branch on a designated fork that acts as the integration point for the changes.
In the example above, John's fork has the shared branch which will be used to do pull requests from Dean's, Mike's, and John's task branches. When all of the work has been completed, one of the developers will have to rebase on the latest changes of the upstream repository and file a pull request to it.
For a more in depth explanation of the example above, read this great blog post A Git collaboration workflow that provides feedback early and fast
Adding Teammate's Fork as Remote
# Set teammate's fork as remotegit remote add THEIR-USERNAME git@github.ibm.com:THEIR-USERNAME/REPO-NAME.git
Getting Teammate's branches locally
# Fetch from teammate's forkgit fetch THEIR-USERNAME

posted @ 2018-01-07 11:48  IronJJ  阅读(815)  评论(0编辑  收藏  举报