Let us consider an example your in team where developer 1 working on feature1,developer2 working on feature 2 etc.
With out branching if the developers push and pull the code daily, which will leads to a confusion and code duplication or removal of code which might impact other features.
So git provides an option to create multiple branches, So that each developer can create there own branch and then update the code, push code to their own branch ,finally merge with main branch.
Eg: Developer 1 creates a new branch name: dev-login
Developer 2 creates another branch name: dev-signup.
So, Developer 1 and Developer 2 can work on their branch and once every thing is done. They will come together to merge the Login and signup branches to main.
Git branching commands:
1. git branch: Use to display all the branches on currently working repo(in our example devopsDemo)
2. git branch --show-current: Display the name of current working branch.
3. git branch branch-name: We can create nee branch using this command.
eg : git branch dev-login ,new branch with dev-login name is created.
4. git switch branch-name: We can switch to another branch using this command.
eg: You are in main branch, want to work on dev-login branch created above. use below command.
git switch dev-login
5. git checkout -b branch-name: If we want to create a new branch and switch to that branch. You can use this command.
eg. I want to created branch name: dev and switching to dev branch from current branch: main.
We can observe that new branch (dev) is created and I am in dev branch now.
5. git branch -d branch-name: Used to delete particular branch.Note: When you're working as team, discuss with other teams members or developer why you want to delete the branch.
6. Branches we created in above steps are created in local repo, there will be only one branch(main) in central repo.
git branch: we can see the branches from local repo.
git branch -r : we can see the branches from central repo.
7. git branch -m old-name new-name: Use to rename the exiting branch.
8. Lets us create a login.txt file in dev-login branch and push the code to central repo. follow the steps shown below.
Adding comments and committing the code.
Pushing the code to central repo.
8. Lets us create a signup.txt file in dev-signup branch and push the code to central repo. follow the steps shown below.
We can commit and push the code from local repo to central repo using below steps:
9. Verify if new branches are created on GitHub. When you open your GitHub account and go to devopsDemo repository ,you can see two new branches are created as below.
10. We can observe that,login.txt is added in dev-login branch.
We can observe that,signup.txt is added in dev-signup branch.
We can see the same thing from your terminal locally.
Will continue with Best practices and merging concepts in next session....
Comments
Post a Comment