Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create and commit a branch in gitlab

This is intended as a question that I answer for other people's benefit. However, if someone answers this question before I finish my research, I would be grateful.

How do I, from the shell, branch an existing git repo (which I have developer access to), edit it, commit those changes then push it to the server for review before being merged.

EDIT

Please note that this is not my project, but someone else's. This use has given me access to do some work. When done, I will request that they merge the changes back to the original

like image 239
puk Avatar asked Aug 20 '15 18:08

puk


People also ask

How do I create a commit branch?

In order to create a Git branch from a commit, use the “git checkout” command with the “-b” option and specify the branch name as well as the commit to create your branch from. Alternatively, you can use the “git branch” command with the branch name and the commit SHA for the new branch.

How do you commit and push to a branch in GitLab?

Create a new branch with the branch, switch or checkout commands. Perform a git push with the –set-upstream option to set the remote repo for the new branch. Continue to perform Git commits locally on the new branch. Simply use a git push origin command on subsequent pushes of the new branch to the remote repo.


2 Answers

For setting up your repo, you will need to follow these instructions. Then you will need to clone/fork the existing repo like this.

Then make your changes. Once you are done making your changes. You will need to make a "commit" that looks like this

git commit -m "I changed something somewhere"

Then you will want to pull down any changes from the repo that may have been pushed while you were working.

git pull origin master   // master being the branch that you cloned/forked

Once the pull is completed with no conflicts, you may push your changes up.

git push origin master   // this is saying that you want to replace the remote master branch with your local master branch

EDIT To push to a repo without overwriting the master, do this:

git clone                   //clone what branch you want
git checkout -b new_branch  //this will create a new local branch
git push origin new_branch  //this will create a new origin branch
like image 182
Sari Rahal Avatar answered Oct 07 '22 10:10

Sari Rahal


If I understand your question correctly:

  • You don't have permission to push to the repository
  • You want to create a branch, make some commits, and then propose a merge request

This is actually a perfectly normal situation, here's what to do:

  1. On GitLab, fork the project: this creates a clone of the original repository in your personal workspace. The point is that you can push to your personal workspace.

  2. On your PC, clone from your fork, not the original.

  3. Create a branch (git checkout -b myfeature), make the changes and commit, then push this branch to your fork (git push -u origin HEAD)

  4. On GitLab, visit your fork's page, and near the top there should be a button offering you to create a Merge Request from the branch that you pushed just now. Click on it, review the changes, if it looks good, then set an assignee and click Create. The assignee should receive an email notification

You don't need write access to a project to be able to contribute. All your write operations are on your workspace on GitLab and on your local PC. Reviewers of your Merge Request can accept it or reject as they wish. They can also ask that you make improvements, which you can implement and push (simple git push after your local commits), that updates the merge request (reviewers can reload the page in the browser and see your changes).

like image 43
janos Avatar answered Oct 07 '22 11:10

janos