Moving to an existing branch
If you want to move your commits to an existing branch, it will look like this:
git checkout existingbranch
git merge master
git checkout master
git reset --hard HEAD~3 # Go back 3 commits. You *will* lose uncommitted work.
git checkout existingbranch
You can store uncommitted edits to your stash before doing this, using git stash
. Once complete, you can retrieve the stashed uncommitted edits with git stash pop
Moving to a new branch
WARNING: This method works because you are creating a new branch with the first command: git branch newbranch
. If you want to move commits to an existing branch you need to merge your changes into the existing branch before executing git reset --hard HEAD~3
(see Moving to an existing branch above). If you don't merge your changes first, they will be lost.
Unless there are other circumstances involved, this can be easily done by branching and rolling back.
# Note: Any changes not committed will be lost.
git branch newbranch # Create a new branch, saving the desired commits
git reset --hard HEAD~3 # Move master back by 3 commits (Make sure you know how many commits you need to go back)
git checkout newbranch # Go to the new branch that still has the desired commits
But do make sure how many commits to go back. Alternatively, you can instead of HEAD~3
, simply provide the hash of the commit (or the reference like origin/master) you want to "revert back to" on the master (/current) branch, e.g:
git reset --hard a1b2c3d4
*1 You will only be "losing" commits from the master branch, but don't worry, you'll have those commits in newbranch!
WARNING: With Git version 2.0 and later, if you later git rebase
the new branch upon the original (master
) branch, you may need an explicit --no-fork-point
option during the rebase to avoid losing the carried-over commits. Having branch.autosetuprebase always
set makes this more likely. See John Mellor's answer for details.
With One Remote
Jakub's answer actually improves on this. With Git versions ≥ 1.6.6, with only one remote, you can do:
git fetch
git checkout test
As user masukomi points out in a comment, git checkout test
will NOT work in modern git if you have multiple remotes. In this case use
git checkout -b test <name of remote>/test
or the shorthand
git checkout -t <name of remote>/test
With >1 Remotes
Before you can start working locally on a remote branch, you need to fetch it as called out in answers below.
To fetch a branch, you simply need to:
git fetch origin
This will fetch all of the remote branches for you. You can see the branches available for checkout with:
git branch -v -a
With the remote branches in hand, you now need to check out the branch you are interested in, giving you a local working copy:
git checkout -b test origin/test
I also created the image below for you to share the differences, look at how to fetch works, and also how it's different to pull:
Best Answer
You need to install Gitlab Plugin on Jenkins.
This will add a
/project
endpoint on Jenkins. (See it in Jenkins => Administrate Jenkins => Configure System => Gitlab )Now add a webhook on your Gitlab project => Settings => Integrations. (or in older Gitlab version : Gitlab project => Wheel icon => Integrations, it seems you need to be owner of project in this case)
In Url : set
http://*yourjenkins.com*/**project**(/*foldername*)?/*yourprojectname*
and that's all, and click on "Add Webhook".When you click on test on your webhook, it should trigger your jenkins pipeline build. (You should have a 200 http response)
It works without authentication in Gitlab plugin, configuration with authentication are welcome.