admin管理员组

文章数量:1335131

I've been trying to figure out how to resolve an issue I've been having for a couple months now and want to confirm what the proper steps are for working on multiple features in a project on github. The steps I've been following are

  1. Clone remote repository to my local computer
  2. Create a new local branch for the feature I want to work on.
  3. Switch to new branch.
  4. Work on feature making atomic commits as I work.
  5. Once I'm done with feature push code to a new feature branch on remote repo.
  6. Go to repo and create pull request for feature to be merge into main branch of remote repo.
  7. Merge local feature branch into local main branch.
  8. While in local main branch create a new feature branch for the next feature I want to work on.
  9. Repeat same process.

The problem I keep running into is if I work on 5 features in a day and make PRs for all of them before someone is able to review the changes and merge them to the remote main branch, each PR contains all the commits and file changes of all the prior PRs rather only showing the files I actually worked on in that feature. I even tried deleting the project locally and starting over with a fresh clone except this time I would merge my local feature branch into my local main branch then push my local main branch into a new remote feature branch and the same problem continues to occur.

I've been added to this project as a contributor with permission to push code directly to their repository, rather than forking it first then make PRs from my forked copy. Even after they accept and merge my changes into the remote main branch and I continue to work on other features all the commits I've made thus far in all PRs still somehow get included in my new PRs. Due to me doing atomic commits it can quickly add up to over 100 changes that they have to sort through to find the most recent changes I made. They say this hasn't happened with any other contributors, if they make changes to 3 files in one PR, then 2 files in another PR, they only see the 3 files in the first PR and 2 files in the 2nd PR. Can someone help me understand what I'm doing wrong or if there's a step I'm missing in my process?

I've been trying to figure out how to resolve an issue I've been having for a couple months now and want to confirm what the proper steps are for working on multiple features in a project on github. The steps I've been following are

  1. Clone remote repository to my local computer
  2. Create a new local branch for the feature I want to work on.
  3. Switch to new branch.
  4. Work on feature making atomic commits as I work.
  5. Once I'm done with feature push code to a new feature branch on remote repo.
  6. Go to repo and create pull request for feature to be merge into main branch of remote repo.
  7. Merge local feature branch into local main branch.
  8. While in local main branch create a new feature branch for the next feature I want to work on.
  9. Repeat same process.

The problem I keep running into is if I work on 5 features in a day and make PRs for all of them before someone is able to review the changes and merge them to the remote main branch, each PR contains all the commits and file changes of all the prior PRs rather only showing the files I actually worked on in that feature. I even tried deleting the project locally and starting over with a fresh clone except this time I would merge my local feature branch into my local main branch then push my local main branch into a new remote feature branch and the same problem continues to occur.

I've been added to this project as a contributor with permission to push code directly to their repository, rather than forking it first then make PRs from my forked copy. Even after they accept and merge my changes into the remote main branch and I continue to work on other features all the commits I've made thus far in all PRs still somehow get included in my new PRs. Due to me doing atomic commits it can quickly add up to over 100 changes that they have to sort through to find the most recent changes I made. They say this hasn't happened with any other contributors, if they make changes to 3 files in one PR, then 2 files in another PR, they only see the 3 files in the first PR and 2 files in the 2nd PR. Can someone help me understand what I'm doing wrong or if there's a step I'm missing in my process?

Share Improve this question asked Nov 20, 2024 at 4:38 OptiqOptiq 3,2645 gold badges41 silver badges80 bronze badges 10
  • 3 Create remote feature_branch for all your features. Push to those features yourself (ofcourse no approval required). Raise merge request for only the features you intend to merge. Two different feature branches will not see each other's changes. So point is, do not merge to main branch in local – Anubhav Sharma Commented Nov 20, 2024 at 4:43
  • @AnubhavSharma ok now I'm more confused. I was told to merge into my local branch because an issue we were having before is say I create a branch to fix a bug in the nav bar and push that code to a new remote branch. When I switch back to my local branch and make a new branch to work on a bug in the footer, the fixes I made to the navbar won't be present. So when I make a PR for the fix to the footer and they merge it into the main branch, it will over write the navbar fix with the old code that needed to be fixed. – Optiq Commented Nov 20, 2024 at 5:00
  • 1 both footer_feature and header_feature would be independent. Let say your footer_feature got merged first and then header_feature. Its still fine unless the change is in the same line. If there is a conflict in the same line, then you need to resolve the conflict manually. How would git know which change to keep? So if you have changes in same line in multiple feature branches, you are perhaps doing it wrong. Combine them into same feature. – Anubhav Sharma Commented Nov 20, 2024 at 5:06
  • 1 Now this is weird, you will not see footer change in header and vice versa. But both features pushed to main branch should be fine. Main branch will definitely contain all the changes. Perhaps someone else tried to merge in between and they chose to not accept your change. – Anubhav Sharma Commented Nov 20, 2024 at 5:41
  • 1 when I switched back to my main branch and created a new feature branch for the footer, the changes from the header branch won't be present Of course; that's expected if you treat the two changes as different features. If you do not like this, then you must declare both changes as a single feature that you fix in one feature branch. – j6t Commented Nov 20, 2024 at 7:14
 |  Show 5 more comments

1 Answer 1

Reset to default 0

The problem arises because each new feature branch is being created from your local main branch, which may not be up-to-date with the remote main branch. If you merge or push changes to your local main branch but the remote main branch has not incorporated those changes yet, all those changes will carry over into subsequent feature branches and PRs.

Best Practices for Multiple Feature Branches Here’s a corrected workflow to ensure feature isolation in PRs:

  1. Update your local main branch regularly Before creating a new feature branch, make sure your local main is up-to-date with the remote main.

    While on main branch

    git checkout main git pull origin main

This ensures you’re working off the latest version of the project.

  1. Create feature branches directly off the updated main Create your new feature branch based on the updated main branch:

    git checkout -b feature/my-new-feature

This ensures the new branch starts with a clean slate.

  1. Work on your feature Make atomic commits and push the feature branch to the remote:

    git add . git commit -m "Add feature details" git push origin feature/my-new-feature

  2. Open a PR for your feature branch Go to the repository and create a pull request targeting the main branch.

  3. Switch back to main after completing the feature After finishing work on a feature branch and creating the PR, switch back to the main branch:

    git checkout main

  4. Update local main again before starting the next feature Always update your local main before starting work on the next feature to ensure it includes the latest changes merged into the remote main:

    git pull origin main

  5. Create a new feature branch from the updated main Now, create the next feature branch following the same process as Step 2:

    git checkout -b feature/next-feature

Addressing Existing PRs with Extra Commits If you already have PRs that include commits from other branches, you can fix them by rebasing the feature branch onto the latest main branch:

Switch to the problematic feature branch:

  git checkout feature/problematic-branch

Rebase the branch onto the latest main:

  git fetch origin
  git rebase origin/main

Force-push the cleaned branch back to the remote repository (since rebasing rewrites history):

  git push --force

This removes unrelated commits from your branch and ensures the PR only contains the intended changes.

本文标签: gitHow to properly push multiple feature branches to remote repoStack Overflow