Bootstrap FreeKB - Git (Version Control) - Merge a branch using the git merge command
Git (Version Control) - Merge a branch using the git merge command

Updated:   |  Git (Version Control) articles

The most basic way to use Git is to use the git clone command to clone an origin Git repository (such as example.git) to a directory on your PC (such as /home/john.doe/git), make a change to a file in the cloned repository on your PC (such as example.txt), use the git commit command to commit the change to the file, and to then use the git push command to upload the file to the origin Git repository.

 

Branches are used as an isolated way to make changes to files in a repository. A common example would be to create a new branch using the git branch or git checkout command, switch to the new branch using the git checkout command, make a change to a file, commit the change using the git commit command, and then merge the branch to the master branch using the git merge command.

 

You can create a branch. The branch will be an identical copy of the files in the master branch. In this example, a branch named development is created.

git branch development

 

The -a or -all flag can be used to display all of the branches in the repository. The * (wildcard) character means you are currently in the master branch.

~]# git branch --all
  development
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

 

The git checkout command can be used to switch to the newly created branch.

~]# git checkout development
Switched to a new branch 'development'

 

Let's make a change to the foo.txt file and then commit the change. Now foo.txt in the master branch contains "Hello World" and foo.txt in the development branch contains "Goodbye World".

echo "Goodbye World" > foo.txt
git commit -m "Goodbye World" foo.txt

 

Switch to the master branch and then the git merge command can be used to merge the development branch to the master branch.

~]# git checkout master
Switched to a new branch 'master'

 

Then the git merge command can be used to merge the development branch to the master branch. Something like this should be displayed. Now foo.txt in the master branch should contain "Goodbye World".

~]# git merge development
Updating ead546c..4a26c57
Fast-forward
 foo.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

 

You may need to also used the git push orign master command.

git push origin master

 


Let's say you have a single file in a branch that you want to merge into another branch. For example, let's say you want to merge bar.txt in the development branch into the master branch. First, ensure you are on the development branch.

~]# git checkout development
Switched to a new branch 'development'

 

Then use the git checkout command with the --patch flag following by the target branch (master in this example) and the file in the currently selected branch (bar.txt) that you want to merge into the target branch.

~]# git checkout --patch master bar.txt
diff --git b/bar.txt a/bar.txt
index f87ad59..0ca1417 100644
--- b/bar.txt
+++ a/bar.txt
- "Hello World"
+ "Goodbye World"
Apply this hunk to index and worktree [y,n,q,a,d,/,e,?]? y

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter f9943c in the box below so that we can be sure you are a human.