In real time, environment,
there are minimum 3 branches.
a. master: Where developers can actively contribute
b. test: Before releasing the product to customer, code from master is
cloned to test branch, and testing done here.
c. production: Customer released code placed here.
Why to maintain
branches?
it is required for developers
to continue their work without messing other branches.
For example, there are 4
developers in a team and each developer is working on different features. If everyone
keeps on committing their changes to master branch, it leads to so many
conflicts. To resolve this, each developer can create their own local branch
from master and contribute to their local branch. Once feature implemented,
developer can do basic testing and merge the changes with master branch.
How to create a
branch from master branch?
First make sure you are in
'master' branch, then execute below command to create new branch from master
branch.
git checkout -b new_branch
How to merge
new_branch changes to master branch?
Make sure you are in master
branch and execute below command to merge the changes from new_branch to master
branch.
git merge new_branch
Let’s visualize the same using
below user interface.
When you launch above uri in
browser, you can see below screen.
Let’s perform two commits by
executing below command twice.
git commit
As you see above image, after
performing first commit, master and HEAD are pointing to new snapshot.
Let me create and checkout
new_branch from master branch by executing below command.
git
checkout -b new_branch
As you see above diagram
‘new_branch’ is created and checkedout (HEAD is below new_branch).
Execute ‘git commit’ statement.
Since we are in new_branch,
when you execute ‘git commit’ statement, it creates new snap shot for
new_branch.
Let’s commit one more time.
Let’s checkout master branch by
executing below command.
git
checkout master
As you see the above diagram,
HEAD is pointing to master branch.
Merge the data from
new_branch to master
If you want merge the data from
new_branch to master branch, first you should be in master (checkout master)
branch and execute 'git merge new_branch' command.
Let’s see the example
practically
Step 1: Clone a git branch.
Syntax
git clone {repository_url}
$git clone https://github.com/harikrishna553/gitHelloWorld.git Cloning into 'gitHelloWorld'... remote: Enumerating objects: 26, done. remote: Counting objects: 100% (26/26), done. remote: Compressing objects: 100% (18/18), done. remote: Total 26 (delta 3), reused 16 (delta 2), pack-reused 0 Unpacking objects: 100% (26/26), done.
My git repository looks like
below.
$tree . └── gitHelloWorld ├── README.md ├── tempFile1.txt ├── tempFile4_copy.txt └── welcome.txt 1 directory, 4 files
Step 2: Add new file ‘master_file.txt’ to the branch.
$cd gitHelloWorld/ $ $touch master_file.txt
Step 3: Commit the changes.
$git status On branch master Your branch is up to date with 'origin/master'. Untracked files: (use "git add <file>..." to include in what will be committed) master_file.txt nothing added to commit but untracked files present (use "git add" to track) $ $git add master_file.txt $ $git commit -m "Adding master file" [master e05ed60] Adding master file 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 master_file.txt $ $git status On branch master Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) nothing to commit, working tree clean
Step 4: Create 'new_branch' from master branch
$git checkout -b new_branch Switched to a new branch 'new_branch' $ $git branch master * new_branch
Step 5: Add new file ‘new_branch_files.txt’ and commit it to new_branch.
$touch new_branch_file.txt $ $git status On branch new_branch Untracked files: (use "git add <file>..." to include in what will be committed) new_branch_file.txt nothing added to commit but untracked files present (use "git add" to track) $ $git add new_branch_file.txt $ $git commit -m "Adding a text file to new_branch" [new_branch 2255b77] Adding a text file to new_branch 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 new_branch_file.txt
Step 6: Merge new_branch to master branch.
Check out master branch
git checkout master
Merge new_branch with master
branch
git merge new_branch
$git checkout master Switched to branch 'master' Your branch is ahead of 'origin/master' by 1 commit. (use "git push" to publish your local commits) $ $tree . ├── README.md ├── master_file.txt ├── tempFile1.txt ├── tempFile4_copy.txt └── welcome.txt 0 directories, 5 files $git merge new_branch Updating e05ed60..2255b77 Fast-forward new_branch_file.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 new_branch_file.txt $ $tree . ├── README.md ├── master_file.txt ├── new_branch_file.txt ├── tempFile1.txt ├── tempFile4_copy.txt └── welcome.txt 0 directories, 6 files $ $git status On branch master Your branch is ahead of 'origin/master' by 2 commits. (use "git push" to publish your local commits) nothing to commit, working tree clean
As you see the output ‘new_branch_file.txt’
is copied to master branch.
Reference
No comments:
Post a Comment