Branches
A branch is just a reference to a commit.
When we initialize git, master branch is created by default and is stored in .git.
To see all the branches in your current repository.
$ git branch
This command will show you all the branches with your current branch having an asterisk sign.
Now in order to see your branches stored in .git. Navigate to .git/refs/heads.
Just move to refs under .git directory.
To see current branch in .git.
Output:
Here, we can see that HEAD in .git is pointing to refs/heads/master.
Now lets create a new for branch and play with it.
$ git branch pubg
Currently we are on master branch. Remember : New branch will have same hash as the master branch. Because content inside the new branch is same as master branch.
Now lets move to branch pubg.
$ git checkout pubg
Verify current branch by
$ git branch
Now lets make some changes in branch pubg. Currently we have following content in addr.txt
addr.txt
Name : Pankaj
address : chandigarh
6th floor, ats heights
After making changes we have following content in addr.txt in pubg branch
name: Pankaj
address : India,chandigarh,sector 32
6th floor, #404 , ats heights
Now, what if details about pankaj in pubg branch is much better. So we need to merge it with master branch. If you want to merge pubg branch with master then first you need to checkout master.
$ git checkout master
Now in order to merge
$ git merge pubg
Sometimes, git generates conflict message saying that "Merge conflict in (file.txt) and asks you to fix it manually. Most of the times you will find this due to confliction of content.
Now if you are VScode fan then you will see following thing in your VScode.
Now you can see four options in VScode
- Accept Current Change - means to keep content of master branch and ignore pubg branch
- Accept Incomming Change - means to kee content of pubg branch and ignore master branch
- Accept Both Changes - To keep both the changes(master's content and pubg's content)
- Compare Changes -Just to compare changes in both branch simultaneously.