DevOps session 13: Automate git SETUP using bash scripting.

Image
  Bash scripting: A Bash Shell Script is a plain text file containing a set of various commands that we usually type in the command line. It is used to automate repetitive tasks on Linux filesystem. It might include a set of commands, or a single command. For example ,to install GIT we need to update the packages, then install git and check if installation is done using Linux commands. We can write all the command in a .sh file and we can execute the file to do all the steps mentioned above. Lets start with example from hello-world. 1.Open terminal and check the root of the bash using below command. CMD: which bash you can see path like  /usr/bin/bash or /bin/bash . 2.Create a hello-world.sh file and paste the below lines. CMD: nano hello-world.sh You can see editor ,add below lines #!/bin/bash # declare MSG variable MSG="Hello World" # print variable on a screen echo $MSG To save the file use CTRL+X, then CTRL+Y and press ENTER 3. To execute the file we need to ad...

DevOps session 12: GIT merging branches/resolving conflicts/tips

 




Before starting with GIT merging session, completed previous session on Git branching .

Let us consider same example from previous session, Developer 1 working on dev-login branch and Developer 2 working on dev-signup branch.

In dev-login branch -login.txt file is created and pushed to GitHub

In dev-sign branch - signup.txt file is created and pushed to GitHub.

In main branch - no files are created

Now we want to combine two features login and signup to main branch. To do that GIT providing us merging concept.

Follow the below steps to merge the branches:

1. First I want to merge the dev-login branch with main branch. Switch to branch where you want to merge the code from other branch(eg: main in my case). use the below command to merge the code from  dev-login branch to  main branch.

You should be on main branch before using below command.

CMD: git merge dev-login


Once merge is successful. You can check if login.txt is created in main branch.

2. Follow the same steps and merge code from dev-signup to main branch. 

You should be on main branch to use below command.

CMD: git merge dev-login

Once merge is successful. You can check if signup.txt is created in main branch.

3. All the changes you are made saved on local repo. If you want to make same changes to your GitHub repo. You need to push the code.

You should be on main branch to use below command.

Follow below steps:


Verify from your GitHub repo:

Before merge and pushing the code:



After merging and pushing the code:



MERGE CONFLICTS:

Merge conflicts means you are facing some issue while merge the code to main or other branches.

Reason:

1.Multiple people using same file created  and making the changes locally.

2.When a file is deleted by another team member.

3.When you created a file, which is already present in another branch and try to push to main branch.


Let us consider an example.

Developer 1 working on dev-login branch 

Developer 2 working on dev-signup branch

Developer 2 created a signup.txt file in dev-signup branch 

Develop2 1 also created signup.txt file in dev-login branch 



Now Developer 1 and Developer 2 want to merge the code from dev-login and dev-signup branches.

Here we will get the conflicts because in same branch no two files will be with same name.



Solution :

You can use git merge tool command to get some hints to resolve the conflicts


Now to resolve the issue, we can switch to dev-login branch and rename the signup.txt file to signup2.txt.



Then commit the code and push to dev-login branch.


As a next step ,switch to main branch and check the signup.txt file if any extra content is added to file. In that case ,we need to edit and remove the extra lines added in the file and save it.

I have use vi editor to to edit the file. follow below steps

cmd: Vi filename: file will open in editor

A console will open, to start insert mode press "i" from keyboard.

Remove extra lines and to exit insert mode ,press "ESC" from keyboard.

Then ,to save and quit the file, we can type " :wq " at the bottom of file.



Use the command git merge dev-login shown above to merge the code to main. There will be no issue and code is merged from dev-login to main with out any issues.

Signup2.txt is copied to main successfully and shown below.



Tips to avoid MERGE Conflicts:

1. Check if another branches if same file is present in another branch ,now we can create a new file or we can use existing file.

2. Start your work by only after getting the latest code.

3.Many people working on same branch. So, you can use the code but don't make any updates the code created by other person with out discussing with them.

4. Make sure that ,you are going to push the latest changes to your team working branch.

5.Dont delete, update a file with out connecting with owner of that file.

Comments

Popular posts from this blog

DevOps session 10: Git basic commands for daily use

DevOps session 7: Source Code Management/ Version control systems (GIT) || Setup Git on ubuntu

DevOps session 11: Git branching & best practices