Advance Git & GitHub for DevOps Engineers: Part 1

Advance Git & GitHub for DevOps Engineers: Part 1

·

4 min read

Git Branching:

  • Git branching enables teams to collaborate on a project more efficiently, as it allows for parallel development and the ability to work on different features simultaneously.

  • Each project starts with a default branch main or master.

  • It also provides a way to experiment with new ideas and easily switch between different versions of the code

Git Revert and Reset:

Git revert and reset are two important commands in the Git version control system that allow developers to undo changes in their repository.

  1. What is git revert?
    It creates a new commit that undoes the changes made in previous commit, effectively rolling back the changes. It does not delete the pervious commit but rather creates a new one that negates its changes.

    1. What is git reset?
      It moves the current branch pointer to a specified commit, effectively resetting the repo to that point in time.It can be used to undo local changes that have not yet been committed or to undo one or more commits in the repository. However, it should be used with caution as it can remove commits permanently and make them unrecoverable.

Git Rebase and Merge:

Git rebase and merge are two ways of integrating changes from one branch to another in Git.

  1. What is git rebase?
    It is a command that lets users integrate changes from one branch to another, and the logs are modified once the action is complete. Git rebase was developed to overcome merging’s shortcomings, specifically regarding logs.

2. What is git merge?
It is a command that combines the changes made in one branch with another branch. It creates a new merge commit that contains the changes made in both branches. It is a non-destructive way of integrating changes and preserving the history of both branches.


Task 1: Add a text file called version01.txt inside the DevOps/Git/ with “This is first feature of our application” written inside. This should be in a branch coming from master. Switch to dev branch ( Make sure your commit message will reflect as "Added new feature").

  • To Add file in DevOps/Git directory
touch DevOps/Git/Version01.txt
  • Now Add Content to it
This is first feature of our application
  • Add and commit the file
sudo git add Version01.txt
sudo git commit -m "Added new feature"

  • Now check out to new branch "Dev"
sudo git branch Dev
sudo git checkout Dev

  • Add some more content to it
This is the bug fix in development branch
  • Add and commit changes in the Dev branch
sudo git add Version01.txt
sudo git commit -m "Added feature2 in development branch"


Task 2: Add new commit in dev branch after adding below mentioned content in DevOps/Git/version01.txt: While writing the file make sure you write these lines

  • 1st line>> This is the bug fix in development branch

  • Commit this with the message “ Added feature2 in development branch”

  • 2nd line>> This is gadbad code

  • Commit this with message “ Added feature3 in development branch

  • 3rd line>> This feature will gadbad everything from now.

  • Commit with message “ Added feature4 in development branch

Restore the file to a previous version where the content should be “This is the bug fix in development branch” [Hint use git revert or reset according to your knowledge]

We have to remove last two lines , to recover use below steps:

  • To Recover to pervious version
sudo git log
  • Copy the commit id which you want to revert and use the below command
sudo git revert a53c29

  • You have successfully reverted back to pervious version of your code


Task 3: Demonstrate the concept of branches with 2 or more branches with a screenshot

Git branching is used to take a clone code and implement new ideas and bug fixes without disturbing the main branch.

To create new branch

sudo git branch Develop

To check whether the branch is created or not

sudo git branch

To switch between branches

sudo git checkout <branch_name>

In our case, we have a branch named "Develop" so,


Task 4: Add some changes to dev branch and merge that branch in master

In Develop branch we have this content in "Version01.txt" file

Now Switch to the main branch and merge the changes of Develop branch

Before merging in Version01.txt file
We have only one line of code in the file

After merging the code from Develop branch

For merging the code from another branch we use

sudo git merge <branch_name>

Here we can see Versio01.txt file has changes and there is 2++ indicates there is 2 insertions (+)

You can do cat Version01.txt to verify changes.

Happy Learning!!

Thanks For Reading :)