Here we are going to look at branching and merging in git. In git branches are context which you can switch between. This makes git a better choice among other version contorling system.
So lets start working with branch.
In your project folder which is already a git repository, if you go to that folder in command line.
Check all branch:
To check all the branches in you repo type:
This will show all the branch available.
So initially you just have one branch master .
Create new branch:
If you want to create a new branch then you need to use command:
git branch name_of_your_new_branch
Say you want to create a branch named newbranch , then the command will be
git branch newbranch
Then if you check your branches with the command git branch , you can see all the branch and the new branch is there. and you can see an asterisk (*) sign on the left of master , that means you care currently using master branch.
Switch to another branch:
Now you if you want to use the new branch, you need to checkout to that branch. The command is:
git checkout newbranch
Now check all your branch with git branch command. You can see newbranch is your current working branch, as it has an asterisk mark on its left.
Adding some file to test merging:
If you add a new file in your new branch say test.php add some lines or edit any existing file. Then commit it.
And switch to master branch, typing git checkout master .
Then edit the same file in another branch.
Or someone else do some diffent editing in same file.
You need to merge it.
To merge another branch (say newbranch) to your current branch (say master), you neet to use git merge command:
git merge newbranch
This will merge newbranch with your current branch.
It will show the list of files that is merged and will show file which has conflict.
Resolve conflict and commit.
you are done….:)
If you want to delete a branch, then for deleting local branch you need git branch command with -d option. type:
git branch -d newbranch
Your branch will be deleted from your local repo.
To remove a remote branch, type:
git push origin :newbranch
You may get an error, like:
error: unable to push to unqualified destination: newbranch
The destination refspec neither matches an existing ref on the remote nor
begins with refs/, and we are unable to guess a prefix based on the source ref.
error: failed to push some refs to 'git@repo_name'
Then someone must have already deleted it.
What you need to do in that situation is to synchronize your branch list. Type:
git fetch -p
The issue should be resolved.