0

There is a remote master in Gitlab [http://something.com/somemaster.git]

I created a remote branch from Gitlab UI in browser [http://something.com/somemaster/mybranch.git]

I cloned the remote branch in my Windows machine [/e/myprojects/mybranch (master)]. I added code to my local branch master and have pushed the code to remote branch.

Meanwhile other people have added code to remote master.

Now, I want to push my changes to the remote master.

How to do that has become very confusing to me. Following is what I think needs to be done.

  1. Merge the changes from remote master to my local branch master
  2. Commit and Push the merged local branch master to my remote branch
  3. Create a merge request so that admin can merge my changes to remote master

If the above are correct, what commands do I need for step#1 and do I run them while I am in the local branch master in git bash? I am more comfortable with merge than rebase.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
ontherocks
  • 1,747
  • 5
  • 26
  • 43

1 Answers1

1

You first need to update remote tracking branches with git fetch. Then you do git merge origin/master. This merges with the copy of the master branch from origin. Instead, you can just do git pull which does both operations in a single command.

While this process might work for some projects, it is common to use so-called "feature branches", especially when working with a team. With this process, you create a new branch for every feature and bug fix. When you finish, you push your branch to Gitlab then create a Merge Request for others to view. Ideally, a Merge Request is the only way to add commits to master.

Gitlab has settings so that you cannot push directly to master. Using this settings helps you to enforce the process I described in the previous paragraph.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • 1
    To reiterate, running `git fetch` in the local branch master git bash prompt will update the local branch master with the **remote branch** and then `git merge origin/master` will update the local branch master with the **remote master**. – ontherocks Sep 22 '21 at 15:13
  • 1
    @ontherocks More accurately `git fetch` updates [the remote tracking branch](https://stackoverflow.com/questions/4693588/what-is-a-tracking-branch), not the local branch. – Code-Apprentice Sep 22 '21 at 15:39
  • 1
    I'm not sure if it's clear (to OP) from the answer that if you want to use Merge Requests, you *must* use feature branches. (Haven't used GitLab but I assume that's true...) – TTT Sep 22 '21 at 15:58
  • @Code-Apprentice oh, the update is other way around then. But that can be achieved by a mere `git push` which I have already done and my remote branch is up to date with respect to my local branch. Is `git fetch` redundant in this case? – ontherocks Sep 22 '21 at 16:16
  • 1
    @TTT If you make MRs within the same repo, that is true. Even if you make MRs across forked versions of the same repo, feature branches are strongly encouraged but aren't technically required. – Code-Apprentice Sep 22 '21 at 16:54
  • @ontherocks If you just do `git pull`, then yes, `git fetch` is redundant. `git push` will send your commits to GitLab, which is related, but not what you were originally asking about. – Code-Apprentice Sep 22 '21 at 16:55
  • @Code-Apprentice Sorry I am getting confused. Lets keep the combined single command `git pull` aside for a moment. I wanted to understand the two separate commands. You've mentioned in the answer that `git fetch` updates the local branches and then in your comment you've mentioned that it updates the remote branch (tracking branch). Which one of the two is correct? – ontherocks Sep 22 '21 at 17:08
  • @ontherocks I have updated the wording to hopefully be more clear along with a link for more details about "remote tracking branches". I can see how the original wording "local copies of remote branches" might be unclear. `git fetch` updates so-called remote tracking branches which are local copies of the remote branches. These are not "local branches". Please read the link in the current version of my answer for more details. – Code-Apprentice Sep 22 '21 at 17:16