ChatGPT解决这个技术问题 Extra ChatGPT

Merge development branch with master

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.

git pull -u sets the upstream tracking for the branch (or all branches if pushing more than one). Once it is set the tracking persists. There is no reason to use it continually.

S
Sailesh

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

There is a sligh drawback on that approach: the actual merge to master is most probably a fast-forward merge and therefore does not create any commit node. This is no problem with the actual code on the branch, but makes it hard to find out later, who did the actual merge to master and at which time. An explicit --no-ff to the merge to master is needed to fix that.
Yes, exactly that is what --no-ff is for. :)
That's git merge --no-ff development just to correct @elect's usage.
@sailesh can you please update your answer to include the git merge flag, if you agree with the comments?
@Mars, merging will override a file if the old change was in direct ancestry of the commit. For example, let A->B->C be master, and A->X->Y is your dev branch. If you change part of a file in X which might have had conflicts with a change in A, it will not be a conflict, because A is an ancestor of X. Regarding lost changes, check out stackoverflow.com/questions/7147680/… to recover any changes.
a
alper

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.


Thanks! This to me make more intuitive sense.
Yes -- in the 6+ years since I wrote this, I too have adopted it -- although with rebase to update dev branch instead of merge.
G
Gediminas

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:

Clone repository in your local dir (or create a new repository):

$ cd /var/www
$ git clone git@bitbucket.org:user_name/repository_name.git

Create a new branch. It will contain the latest files of your master branch repository

$ git branch new_branch

Change your current git branch to the new_branch

$ git checkout new_branch

Do coding, commits, as usual…

$ git add .
$ git commit -m “Initial commit”
$ git push # pushes commits only to “new_branch”

When job is finished on this branch, merge with “master” 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.


I liked your approach of not working on master. but today when i was playing with gitflow, i created release branch our of the develop. Then added a release note file and committed. Then finished the release which merges back to both master/develop. but my master branch only had release note newly added in it. no other files during previous develop commits were updated in it.
if you work on another branch than master, be sure you have commited and pushed changes to that branch. Than you can look how the files look like on Graphic interface of github.com or bitbucket.com and try to click Merge there, on the website. It should update everything from your branche to the master. If master has newer files, it should be a conflict and you will get the error message. Not sure I have answered good enough, please give me message if not :)
I am using sourcetree as GUI and github repository. I tried 2 times with release test. the master never updated with the latest develop branch.
try to use on live github.com website the files of your branch you are working on. Are they pushed? If yes, try to click on the same branch - Merge and you will see what happens. In my personal experience with sourcetree is quite bad - I wasn't able to understand fully what is happening in my branches too
Thanks @Gediminas for detailed explanation. I was confused in git keywords before reading your answer.. :)
s
sandip
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

s
sandip

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:

setup the git-flow project

create branches and merge everything to develop

run the command git flow release start

then provide a meaningful message for the release

run the command git flow release finish

it will merge everything into master and change the branch to master.

run the command git push to publish the changes to the remote master.

For more information, visit the page - http://danielkummer.github.io/git-flow-cheatsheet/


The solution if someone uses git flow!
S
Sergiu Dumitriu

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.


H
Haris Np

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

What here is specific to Mac or Ubuntu?
Sorry. None of the other answers mentioned that the commands should be given in the Terminal and the command for deleting the branch. Actually I just wanted to add the command for deleting the branch so that the developer don't mess with the same branch in the future. I am using Mac, so I mentioned it. Your question is valid and none of these commands are specific to Mac or Ubuntu.
Thanks for clarifying.
k
kataras

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.

N
Nesha Zoric

This is how I usually do it. First, sure that you are ready to merge your changes into master.

Check if development is up to date with the latest changes from your remote server with a git fetch Once the fetch is completed git checkout master. Ensure the master branch has the latest updates by executing git pull Once the preparations have been completed, you can start the merge with git merge development Push the changes with git push -u origin master and you are done.

You can find more into about git merging in the article.


C
Cisco

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


B
Bhanuchander Udhayakumar

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.


Does this push all the development commits to master as well, or simply add a new single commit into master?
how does this actually work? specifically "git branch master" when you are on the develop looks like madness. how can you create a new branch called master, if there is already a branch called master? docs say that -f does this: Reset to . What does this mean?
Isn't this force pushing local master onto remote master? This seems like a bad idea if you are working in a team.
-f is not recommended.
T
Tiago Medici

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 

S
Sankaran Srinivasan

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


d
djm.im

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


A
Ankit Kumar Rajpoot
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

Force-pushing when you see that error is almost never the correct thing to do, unless you are very certain that you know why your local branch is missing commits from the remote branch. In general, it is much better to go back to step 3 and pull again. Also, it is not clear what value this answer adds compared to existing answers.