Branching in Git
Creating a New Branch
Creating a branch is straightforward. Here's how to create a new branch named "new-branch1"
git branch #On which branch you are right now
git branch new-branch1 #Create new branch
git checkout new-branch1 #switch to branch
You can also create and switch to a new branch in one line using the -b
flag
git checkout -b new-branch2
Merging Branches
Once you're done making changes on your branch, you might want to incorporate those changes into your main branch. Here's how to do it:
git checkout master
git merge new-branch1
Here we will Create and Resolve Merge Conflicts
Sometimes, Git can't automatically merge branches due to conflicting changes in the same part of a file. Git will tell you that a conflict has occurred and it's up to you to resolve it. Open the file with the conflict, and you'll see something like this:
- Create a new Git repository: Navigate to your desired directory and initialize a new Git repository using
git init
.
mkdir git-demo
cd git-demo
git init
- Create a file: Use a text editor to create a file (say
file.txt
) and add some content to it.
echo "Hello World" > file.txt
- Add and commit the file: Add the file to Git and commit it.
git add file.txt
git commit -m "Initial commit"
- Create a new branch: Let's create a new branch called
new-feature
.
git checkout -b new-feature
- Modify the file on the new branch: Open
file.txt
and change "Hello World" to "Hello Git".
echo "Hello Git" > file.txt
git commit -am "Change text to 'Hello Git'"
- Switch back to the master branch:
git checkout master
- Modify the same file on the master branch: This time, change "Hello World" to "Hello OpenAI".
echo "Hello OpenAI" > file.txt
git commit -am "Change text to 'Hello OpenAI'"
Now, we've set up a merge conflict. Both branches have changes on the same line in the same file but with different content. Let's attempt to merge the new-feature
branch into master
and resolve the conflict.
- Attempt to merge the branches:
git merge new-feature
git log --oneline
Git will output a message saying that there is a conflict.
- Open the conflicted file: Open
file.txt
, and you will see something similar to:
<<<<<<< HEAD
Hello OpenAI
=======
Hello Git
>>>>>>> new-feature
The content between <<<<<<< HEAD
and =======
is the version of the current branch (master
), and the content between =======
and >>>>>>> new-feature
is the version from the new-feature
branch.
- Resolve the conflict: Decide which version to keep, or merge the changes manually. For instance, you can edit the file to say "Hello OpenAI and Git" to include both changes.
Hello OpenAI and Git
Save and close the file.
- Commit the resolved conflict: After resolving the conflict, add the file to the staging area and commit it.
git add file.txt
git commit -m "Resolved merge conflict"
Now you've successfully resolved a Git merge conflict! ๐ฅ
Delete a New Branch
After successfully merging your changes, you may want to delete the branch to avoid cluttering your repository. Use the -d
option:
git branch -d new-feature
Conclusion
Git branching is a vital tool for any developer. It promotes a seamless workflow, encourages experimentation, and enhances collaborative work. As you become more comfortable with it, you'll find it an invaluable part of your coding toolkit. Remember, the power of Git branching comes from understanding how to use it effectively.
#git #github #bash #merge #gitmerge #gitconflict #conflict #resolve #branching #gitbranch #deletebranch #devops #trainwithshubham #90daysofdevops #lovemyjob