Created
July 14, 2023 19:10
-
-
Save maylortaylor/4433cab134751bfdd60252e49e667499 to your computer and use it in GitHub Desktop.
Git Flow Using Rebase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Git Flow Using Rebase | |
At a high-level, the workflow can be described in a few steps: | |
1. Fetch upstream changes. | |
* Always be working with the latest version of the codebase | |
```bash | |
git fetch upstream | |
``` | |
2. Merge upstream/master branch into local master branch. | |
* Before you create a local branch off of your local master branch, you should merge `upstream/master` into master so that you have the latest code. | |
```bash | |
git checkout master | |
git merge upstream/master | |
``` | |
3. Create a branch. | |
* Now that `master` is up to date, create a local branch to track the work for your issue | |
```bash | |
git checkout -b issue-name-1 | |
``` | |
4. Write code and commit to your branch as you go. | |
```bash | |
git commit -m "MDA2-{ticket-number} some detailed | |
message about what you accomplished in this commit" | |
``` | |
5. Fetch from upstream again (in case upstream master has had new commits since you started your branch). | |
* Once your coding is complete, be sure to grab any new commits that have appeared in `upstream` (you may not be the only person working on this project!) | |
* To get the new `upstream` commits: | |
```bash | |
git fetch upstream | |
``` | |
* Now your local `upstream/master` branch contains any new commits that are in upstream's master branch. | |
6. Rebase and squash your branch against upstream/master, resolving any merge conflicts. | |
* Rebasing will change the original commit on which a branch is based. Rebasing will result in new commits (with the same commit messages) with new SHA-1 hashes. Squashing will condense commits into a new commit (or commits) with a new SHA-1 hash. Typically you’ll want to rebase against the branch that you intend to merge into. When you eventually create your pull request, it will be from your fork’s branch to upstream’s master branch. Therefore, you’ll want to rebase against upstream/master. | |
```bash | |
git rebase upstream/master | |
``` | |
* As rebase processes your commits, it may run into a merge conflict (for example, if you and upstream changed the same part of a file). If this happens, rebase will pause and wait for you to manually resolve the conflict. To do this, simply run `git status` to find out which file(s) have conflicts and then go into each one and resolve them. Once you're done fixing merge conflicts, run ONE of the following: | |
```bash | |
# choose ONE of these commands that suites your needs | |
git add . # Add only files created/modified to the index and not those deleted | |
git add -u # Add only files deleted/modified to the index and not those created | |
git add -A # Do both operations at once, add to all files to the index | |
``` | |
* Then run: | |
```bash | |
git rebase --continue | |
``` | |
7. Push your branch. | |
* In order to create a pull request you need to push your branch to `origin` (your fork of the `upstream` project). | |
```bash | |
git push --set-upstream origin issue-name-1 | |
``` | |
* NOTE: If you’ve already pushed your branch and need to update it, the above command will fail. Since a rebase rewrites commit history, you will no longer have a common commit on your branch and must use the --force option to instruct Git to discard the branch on your remote: | |
```bash | |
git push --force origin issue-name-1 | |
``` | |
8. Open a pull request in Gitlab -- [Gitlab MDA-Next-UI Merge Request](https://gitlab.com/polestardefense/mda-next/mda-ui/-/merge_requests) | |
* From here, you’re ready to open a pull request from your fork’s (origin) feature branch (issue-1) to the upstream repository’s master branch. If you make any changes to your branch, just follow steps five through seven. When you push to a branch, your pull request automatically includes all changes. | |
9. Delete old local feature branch | |
* Once the pull request is accepted, you can delete both your local feature branch and the branch on your fork (origin). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment