How to Commit a Merge Request?

What ever we use the component development or contribute a code to an open source project, I recommend using the rebase to keep our git tree clear. There are some troubles when I use the rebase command in my work that I need note them here.

The steps to commit a merge request

  1. Fork the target repository https://github.com/target/project
  2. Yourself forked repository https://github.com/ssbun/project
  3. Clone yourself repository to local.
1
git clone origin https://github.com/ssbun/project
  1. Checkout a new branch feature-xxx
1
git checkout -b feature-xxx
  1. Write code in this branch.
  2. Commit your code.
1
2
git add .
git commit -m "message"
  1. Push your new commit to your remote repository.
1
git push origin feature-xxx
  1. You can find the returned message has a merge request url.
  2. Open the link that you can commit a merge request to the target repository.

Sync the target repository to local

Sometimes you need a long time to develope the new features and the target repository might accept other merge requests in this period. You commit a MR this moment directly most likely occurs conflicting.

Usually, we would multiple merge the target repository to sync our local code. All the merge logs will be recorded in our git tree and these insignificant merge records will dirty our git history.

Fortunately, the git provides a rebase command can resolve the trouble we met. Using rebase can pull all commits from the target repository to local without any rebase logs.

  1. Add the target repository remote url
1
2
3
4
5
git remote add upstream `https://github.com/target/project`
# Executes the `git remote`. The output:
#
# origin
# upstream
  1. Rebase the target repository to local
1
git pull upstream dev --rebase
  1. Resolve all conflicts
  2. Push you code to your remote repository
1
git push origin feature-xxx

Then, you would get an error that requires you git pull origin feature-xxx fist.
Don’t do this. If you execute this command you will get a chaotic git records and then you can only recreate a new MR to fix it.

To resolve this problem is very easy that you need to add a -f flag to your push command. The -f means force publishing. Don’t worry that the force publishing would break your code, it’s ok.

  1. Force publish your code
1
git push -f origin feature-xxx
  1. Then you will get a link from the result message that point to the MR page