Advance Git & GitHub for DevOps Engineers

Advance Git & GitHub for DevOps Engineers

What is Git Branching?

Git branching is a feature of the Git version control system that allows developers to work on multiple versions of a codebase simultaneously. A branch in Git is essentially a pointer to a specific commit in the repository's history. When a developer creates a branch, they can make changes to the code without affecting the main codebase.

Git branching allows developers to work collaboratively on a codebase without interfering with each other's work. Developers can create their own branches, work on them independently, and merge their changes back into the main codebase when they are ready. This helps to keep the codebase organized and reduces the risk of conflicts between developers.Git branching also provides a way to maintain multiple versions of a codebase simultaneously.

What is Git Revert and Reset?

Git revert and reset are two commands in the Git version control system that allow developers to undo changes made to a codebase. While both commands are used for undoing changes, they work in different ways and have different use cases.

Git revert creates a new commit that undoes the changes made in a previous commit. When a developer reverts a commit, Git creates a new commit that is the exact opposite of the original commit. This means that the changes made in the original commit are undone, but the commit itself is still in the repository's history. This is useful when the changes made in a commit need to be undone, but the commit itself is still important to the repository's history.

Git reset, on the other hand, allows developers to reset the codebase to a previous state. When a developer resets the codebase, Git removes all the changes made since the specified commit and returns the codebase to the state it was in at that commit. This can be useful when a developer wants to discard all the changes made since a certain point and start over from there.

What is Git Rebase and Merge?

Git rebase and merge are two commands in the Git version control system that are used to integrate changes made in one branch into another branch. While both commands are used to integrate changes, they work in different ways and have different use cases.

Git merge integrates changes made in one branch into another branch by creating a new commit that combines the changes from both branches. When a developer merges a branch into another branch, Git creates a new commit that combines the changes made in both branches. This is useful when the changes made in one branch need to be incorporated into another branch without losing any of the changes made in either branch.

Git rebase, on the other hand, integrates changes made in one branch into another branch by moving the changes to the top of the target branch. When a developer rebases a branch onto another branch, Git moves all the changes made in the rebased branch to the top of the target branch. This makes it look like the changes made in the rebased branch were made on top of the changes made in the target branch. This is useful when a developer wants to incorporate changes made in one branch into another branch without creating a merge commit.

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, [hint try git checkout -b dev], swithch to dev branch ( Make sure your commit message will reflect as "Added new feature"). [Hint use your knowledge of creating branches and Git commit command]

  • version01.txt should reflect at local repo first followed by Remote repo for review. [Hint use your knowledge of Git push and git pull commands here]

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 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]

To add a text file called version01.txt inside the Devops/Git/ with “This is first feature of our application” written inside, you would need to follow these steps:

  1. Open your terminal and navigate to the Devops/Git/ directory using the "cd" command.

     user@ubuntu:~$ cd /home/user/Devops/Git/
     user@ubuntu:~/Devops/Git$ pwd
     /home/user/Devops/Git
    
  2. Create a new branch called "dev" using the "git checkout -b dev" command.

     user@ubuntu:~/Devops/Git$ git checkout -b dev
    
  3. Create a new file called "version01.txt" using the "touch" command.

     user@ubuntu:~/Devops/Git$ touch version01.txt
    
  4. Open the file using your preferred text editor and add the following line: "This is first feature of our application".

     user@ubuntu:~/Devops/Git$ nano version01.txt
    
     user@ubuntu:~/Devops/Git$ cat version01.txt
     This is first feature of our application
    
  5. Save the file and then add it to the local Git repository using the "git add" command.

     user@ubuntu:~/Devops/Git$ git add version01.txt
    
     user@ubuntu:~/Devops/Git$ git status
     On branch dev
     Changes to be committed:
       (use "git restore --staged <file>..." to unstage)
             new file:   version01.txt
    
  6. Commit the changes to the dev branch using the "git commit" command with a commit message of "Added new feature".

     user@ubuntu:~/Devops/Git$ git commit -m "Added new feature"
     [dev 9b6af2c] Added new feature
      1 file changed, 1 insertion(+)
      create mode 100644 version01.txt
    
  7. Push the changes to the remote repository using the "git push" command.

user@ubuntu:~/Devops/Git$ git push origin dev
user@ubuntu:~/Devops/Git$ git log origin/dev
commit 9b6af2c4b4e8c04a109e00a13b0761f31ee1d2e2 (HEAD -> dev, origin/dev)
Author: yourname <youremail@example.com>
Date:   Sun Mar 26 17:45:37 2023 +0530

    Added new feature

To add new commit in dev branch after adding the below-mentioned content in Devops/Git/version01.txt:

Open the "version01.txt" file using your preferred text editor and add the following lines:

"This is the bug fix in development branch"

"This is gadbad code"

"This feature will gadbad everything from now."

user@ubuntu:~/Devops/Git$ nano version01.txt
user@ubuntu:~/Devops/Git$ cat version01.txt
This is first feature of our application
This is the bug fix in development branch
This is gadbad code
This feature will gadbad everything from now.

Save the file and then add it to the local Git repository using the "git add" command.

user@ubuntu:~/Devops/Git$ git add version01.txt
  1. Commit the changes to the dev branch using the "git commit" command with a commit message of "Added feature2 in development branch".

     user@ubuntu:~/Devops/Git$ git commit -m "Added new feature"
     [dev 9b6af2c] Added feature2
      1 file changed, 1 insertion(+)
      create mode 170644 version01.txt
    
  2. Push the changes to the remote repository using the "git push" command.

     user@ubuntu:~/Devops/Git$ git push origin dev
    
user@ubuntu:~/Devops/Git$ git log origin/dev
commit 9b6af2c4b4e8c04a109e00a13b0761f31ee1d2e2 (HEAD -> dev, origin/dev)
Author: yourname <youremail@example.com>
Date:   Sun Mar 26 17:50:37 2023 +0530

    Added feature2

To restore the file to a previous version where the content should be "This is the bug fix in development branch":

  1. Find the commit hash of the commit that contains the content you want to restore by using the "git log" command.

  2. Use the "git revert" command followed by the commit hash to revert the commit and restore the file to its previous version. Alternatively, you could use the "git reset" command to reset the branch to the previous commit and then force push the changes to the remote repository.

    To do this, we would use the following command:

     git revert abc123
    

    This will open up the Git commit message editor, allowing you to modify the commit message for the new revert commit. Once you have saved and closed the commit message editor, Git will create a new commit that undoes the changes made in the original commit.

    Here is the output you might see when running this command:

     [master 321cba] Revert "Add feature A"
      1 file changed, 1 deletion(-)
    

#Git #DevOps #Github

Please free to write your thoughts and can connect with me on LinkedIn