Reputation: 32331
I have two branches namely master
and development
in a GitHub Repository. I am doing all my development in development branch as shown.
git branch development
git add *
git commit -m "My initial commit message"
git push -u origin development
Now I want to merge all the changes on the development
branch into the master
. My current approach is:
git checkout master
git merge development
git push -u origin master
Please let me know if the procedure I am following is correct.
Upvotes: 958
Views: 1128459
Reputation: 5480
Personally, my approach is similar to yours, with a few more branches and some squashing of commits when they go back to master.
One of my co-workers doesn't like having to switch branches so much and stays on the development branch with something similar to the following all executed from the development branch.
git fetch origin master
git merge master
git push origin development:master
The first line makes sure he has any upstream commits that have been made to master since the last time updated his local repository.
The second pulls those changes (if any) from master into development
The third pushes the development branch (now fully merged with master) up to origin/master.
I may have his basic workflow a little wrong, but that is the main gist of it.
Upvotes: 133
Reputation: 7391
Explanation from the bottom for ones who came here without any knowledge of branches.
Basic main/master branch development logic is: You work only on another branches, so you use main/master branch only to merge with another branch which is ready for merging.
You begin to create a new branch in this way:
$ cd /var/www
$ git clone [email protected]:user_name/repository_name.git
$ git branch new_branch
$ git checkout new_branch
$ git add .
$ git commit -m “Initial commit”
$ git push # pushes commits only to “new_branch”
$ git merge master
$ git checkout master # goes to master branch
$ git merge development # merges files in localhost. Master shouldn’t have any commits ahead, otherwise there will be a need for pull and merging code by hands!
$ git push # pushes all “new_branch” commits to both branches - “master” and “new_branch”
I also recommend using the Sourcetree App to see visual tree of changes and branches.
Upvotes: 51
Reputation: 320
I think the easiest solution would be
git checkout master
git remote update
git merge origin/Develop -X theirs
git commit -m commit -m "New release"
git push --recurse-submodules=check --progress "origin" refs/heads/Master
This also preserves the history of all the branches in use
Upvotes: 1
Reputation: 5600
1. //push the latest changes of current development branch if any
git push (current development branch)
2. //switch to master branch
git checkout master
3. //pull all the changes if any from (current development branch)
git pull origin (current development branch)
4. //Now merge development into master
git merge development
5. //push the master branch
git push origin master
Error
To https://github.com/rajputankit22/todos-posts.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/rajputankit22/todos-posts.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Then Use
5. //push the master branch forcefully
git push -f origin master
Upvotes: -7
Reputation: 901
1. //pull the latest changes of current development branch if any
git pull (current development branch)
2. //switch to master branch
git checkout master
3. //pull all the changes if any
git pull
4. //Now merge development into master
git merge development
5. //push the master branch
git push origin master
Upvotes: 48
Reputation: 816
It would be great if you can use the Git Flow workflow. It can merge develop branch into master easily.
What you want to do is just follow the git-flow instruction mentioned here:
STEPS:
git flow release start <version_number>
git flow release finish <version_number>
git push
to publish the changes to the remote master.For more information, visit the page - http://danielkummer.github.io/git-flow-cheatsheet/
Upvotes: 26
Reputation: 856
If you are using gerrit, the following commands work perfectly.
git checkout master
git merge --no-ff development
You can save with the default commit message. Make sure, the change id has been generated. You can use the following command to make sure.
git commit --amend
Then push with the following command.
git push origin HEAD:refs/for/refs/heads/master
You might encounter an error message like the below.
! [remote rejected] HEAD -> refs/for/refs/heads/master (you are not allowed to upload merges)
To resolve this, the gerrit project admin has to create another reference in gerrit named 'refs/for/refs/heads/master' or 'refs/for/refs/heads/*' (which will cover all branches in future). Then grant 'Push Merge Commit' permission to this reference and 'Submit' permission if required to Submit the GCR.
Now, try the above push command again, and it should work.
Credits:
https://github.com/ReviewAssistant/reviewassistant/wiki/Merging-branches-in-Gerrit
https://stackoverflow.com/a/21199818/3877642
Upvotes: 0
Reputation: 2204
Once you 'checkout' the development branch you ...
git add .
git commit -m "first commit"
git push origin dev
git merge master
git checkout master
git merge dev
git push origin master
Upvotes: 2
Reputation: 4799
1) On branch Development, check git status using following command:
git status
There should be no uncommitted code. If it is, push your code on Development branch:
git add *
git commit -m "My initial commit message"
git push origin Development
2) On Development branch, run following two commands:
git branch -f master HEAD
git push -f origin master
It will push your Development branch code to master branch.
Upvotes: 3
Reputation: 6640
This is how I usually do it. First, sure that you are ready to merge your changes into master.
git fetch
git checkout master
.git pull
git merge development
git push -u origin master
and you are done.You can find more into about git merging in the article.
Upvotes: 5
Reputation: 31
Based on @Sailesh and @DavidCulp:
(on branch development)
$ git fetch origin master
$ git merge FETCH_HEAD
(resolve any merge conflicts if there are any)
$ git checkout master
$ git merge --no-ff development (there won't be any conflicts now)
The first command will make sure you have all upstream commits made to remote master, with Sailesh response that would not happen.
The second will perform a merge and create conflicts that you can then resolve.
After doing so, you can finally checkout master to switch to master.
Then you merge the development branch onto the local master. The no-ff flag will create a commit node in master for the whole merge to be trackable.
After that you can commit and push your merge.
This procedure will make sure there's a merge commit from development to master that people can see, then if they go look at the development branch they can see the individual commits you've made to that branch during its development.
Optionally, you can amend your merge commit before you push it, if you want to add a summary of what was done in the development branch.
EDIT: my original answer suggested a git merge master
which didn't do anything, it's better to do git merge FETCH_HEAD
after fetching the origin/master
Upvotes: 3
Reputation: 790
If you are on Mac or Ubuntu, go to the working folder of the branch. In the terminal
suppose harisdev is the branchname.
git checkout master
if there are untracked or uncommitted files you will get an error and you have to commit or delete all the untracked or uncommitted files.
git merge harisdev
git push origin master
One last command to delete the branch.
$ git branch -d harisdev
Upvotes: 7
Reputation: 843
Step 1
Create and switch to a new "dev" branch, where your local git files are in-synced with the remote but "dev" branch does not exist yet.
git branch dev # create
git checkout dev # switch
# No need to git add or git commit, the current
# branch's files will be cloned to the new branch by-default.
git push --set-upstream origin dev # push the "dev" branch to the remote.
Step 2
Make your changes to the "dev" branch (your current if you follow step 1), commit and push them to the remote "dev" branch.
git add .
git commit -S -m "my first commit to the dev branch" # remove the -S if you're not "secure", secure = when you already setup crypto private and public keys (i.e "verified" green sign in github)
git push -u origin dev # push the changes to the remote, -u origin dev is optional but good to use.
Step 3
Merge your "dev" branch into the "master".
git checkout dev # switch to "dev" branch if you're not already.
git merge master # optionally, this command is being used to resolve any conflicts if you pushed any changes to your "master" but "dev" doesn't have that commit.
git checkout master # switch to "master", which is the branch you want to be merged.
git merge --no-ff dev # merge the "dev" branch into the "master" one.
Upvotes: 7
Reputation: 26207
I generally like to merge master
into the development
first so that if there are any conflicts, I can resolve in the development
branch itself and my master
remains clean.
(on branch development)$ git merge master
(resolve any merge conflicts if there are any)
git checkout master
git merge development (there won't be any conflicts now)
There isn't much of a difference in the two approaches, but I have noticed sometimes that I don't want to merge the branch into master
yet, after merging them, or that there is still more work to be done before these can be merged, so I tend to leave master
untouched until final stuff.
EDIT: From comments
If you want to keep track of who did the merge and when, you can use --no-ff
flag while merging to do so. This is generally useful only when merging development
into the master
(last step), because you might need to merge master
into development
(first step) multiple times in your workflow, and creating a commit node for these might not be very useful.
git merge --no-ff development
Upvotes: 1439
Reputation: 11611
Yes, this is correct, but it looks like a very basic workflow, where you're just buffering changes before they're ready for integration. You should look into more advanced workflows that git supports. You might like the topic branch approach, which lets you work on multiple features in parallel, or the graduation approach which extends your current workflow a bit.
Upvotes: 11