Day 10: Advance Git & GitHub for DevOps Engineers

Moiz AsifMoiz Asif
4 min read

Git Branching

Git branching allows for a structured and organized approach to software development. By working on isolated branches, developers can avoid conflicts, experiment with new features, and maintain a stable main codebase. It's a powerful technique that facilitates collaboration and efficient code management.

Git Revert and Reset

Sometimes, you may want to undo some of the changes you made in your repository.

Git Revert:

git revert creates a new commit that undoes the changes introduced by a specific commit. It's a safe way to undo changes without altering the commit history. Reverting a commit is ideal for cases where you want to keep a record of the original change and explain why it was undone.

Syntax:

git revert <commit_hash>

Example:

git revert abc123   # Revert the commit with hash "abc123"

Git Reset:

git reset is a more powerful command that can be used to undo commits and manipulate the commit history. It moves the branch pointer to a different commit, effectively erasing commits from the branch's history. It's important to use with caution, especially when working in shared repositories, as it can disrupt collaboration.

There are three main modes of git reset:

  1. Soft Reset (--soft):

    • Moves the branch pointer to a specific commit but leaves the changes staged (i.e., ready to be committed again).

    • Useful when you want to recommit the changes with some modifications.

  2. Mixed Reset (Default, --mixed):

    • Moves the branch pointer to a specific commit and unstages the changes, but retains the changes in your working directory.

    • Useful when you want to review the changes before committing them again.

  3. Hard Reset (--hard):

    • Moves the branch pointer to a specific commit and discards all changes in the working directory and staging area.

    • Be cautious with this mode, as it permanently removes changes.

Syntax:

git reset [--soft | --mixed | --hard] <commit_hash>

Example:

git reset --soft abc123   # Soft reset to commit "abc123"
git reset --mixed xyz456  # Mixed reset to commit "xyz456"
git reset --hard def789   # Hard reset to commit "def789"

Git Rebase and Merge

Git Rebase:

Rebasing involves moving the entire history of a branch to a new starting point. It effectively replays the commits of one branch on top of another. Rebasing can create a linear, cleaner commit history, but it can also rewrite commit hashes, potentially causing conflicts.

Syntax:

git rebase <base_branch>

Example:

# While on the feature branch
git rebase main   # Rebase the feature branch onto the main branch

Git Merge:

Merging combines changes from one branch into another, creating a new commit that has multiple parent commits. Merge commits explicitly indicate when a branch was merged into another.

Syntax:

git merge <branch_to_merge>

Example:

# While on the main branch
git merge feature   # Merge the feature branch into the main branch

Choosing Between Rebase and Merge:

  • Use git rebase when you want to maintain a cleaner, more linear commit history and are willing to address potential conflicts.

  • Use git merge when you want to preserve a clear record of branch integration and have a more straightforward approach to handling changes from multiple branches.

Task:

  1. Switch to dev branch by this command git checkout -b dev

  2. Add a text file called version01.txt inside the Devops/Git/ with “This is the first feature of our application” written inside.

  3. version01.txt should reflect at the local repo first followed by the Remote repo for review.

  4. Your commit message should be “Added new feature”.
    git commit -m "Added new feature”

  5. We will use git push in order to reflect the local changes to the remote repository.

  1. Add a new commit in dev branch after adding the below-mentioned content in Devops/Git/version01.txt: While writing the file make sure you write these lines

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

  3. Commit this with message “Added feature2 in development branch”

  4. 2nd line>> This is gadbad code

  5. Commit this with the message “Added feature3 in the development branch”

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

  7. Commit with the message “Added feature4 in the development branch”

  8. Restore the file to a previous version where the content should be “This is the bug fix in the development branch”. We can use git revertor git reset

Conclusion

In this blog post, you have learned some of the advanced skills and techniques for using Git and GitHub as a DevOps engineer.

You learned how to:

  • Create and manage branches for different features and bug fixes

  • Revert and reset your changes to a previous state

  • Rebase and merge your branches to keep your history clean and avoid conflicts

Thank you for reading!

0
Subscribe to my newsletter

Read articles from Moiz Asif directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Moiz Asif
Moiz Asif