Merging Theory and Practice in Advanced Git Workflows for DevOps

Muzammil Jan
5 min readAug 23, 2024

Git Stash

Git Stash allows you to temporarily save changes that you aren’t ready to commit yet. It removes the changes from your working directory and stores them in a stash stack. You can later retrieve and apply these changes using git stash pop. This is particularly useful when you need to switch branches or work on something else without losing your progress. You can also list all stashes with git stash list.

Git Cherry-pick

Git Cherry-pick lets you apply a specific commit from another branch onto your current branch. This is helpful when you want to selectively bring changes without merging entire branches. You use git cherry-pick <commit-hash> to copy the desired commit. It creates a new commit on the current branch with the changes from the specified commit, preserving the original history.

Git Conflict

Git Conflicts occur when Git is unable to automatically merge changes from different branches. This typically happens when two branches modify the same lines of a file differently. Resolving a conflict involves manually editing the conflicting files, choosing which changes to keep, and then marking the conflict as resolved with git add. After resolving conflicts, a new commit is created to complete the merge process.

Task-01: Stashing Changes and Applying Them Later

Step 1: Create a New Branch

  1. Open your terminal.
  2. Create and switch to a new branch (e.g., feature-branch)
git checkout -b feature-branch

Step 2: Make Changes in the Python File

  1. Let’s say you have a simple Python file named example.py with the following content:
def greet():
print("Hello, World!")
greet()

2. Modify the file by adding a new function in feature-branch. Update the example.py file like this:

def greet():
print("Hello, World!")
def farewell():
print("Goodbye, World!")
greet()
farewell()

Step 3: Stash the Changes

  1. After making changes, save them without committing:
git stash
Changes Stashed

This will stash your changes and revert the working directory to the previous commit.

Step 4: Switch to a Different Branch

  1. Switch to another branch (e.g., main):
git checkout main

Step 5: Make Changes and Commit Them

  1. Make changes to the Python file in the main branch. For example, modify the greet function:
def greet():
print("Hi there, World!")
greet()

2. Stage and commit the changes:

git add example.py
git commit -m "Updated greeting message"

Step 6: Apply Stashed Changes with git stash pop

  1. Now bring back the changes you stashed earlier:
git stash pop
Applied Stashed Changes

This will apply the stashed changes on top of the commits in the main branch.

Step 7: Review and Commit the Combined Changes

  1. Your example.py file should now look like this after combining both changes:
def greet():
print("Hi there, World!")
def farewell():
print("Goodbye, World!")
greet()
farewell()
verified

2. Review the changes with git status.

3. If everything looks good, stage and commit the combined changes:

git add example.py
git commit -m "Added farewell function and updated greeting message"

Task-02: Making Commits in the Development Branch and Rebasing onto Production

Step 1: Switch to the development Branch

  1. Ensure you are on the development branch:
git checkout development

Step 2: Modify version01.txt and Make the First Commit

  1. Open version01.py.
  2. Add the required code changes after the relevant section.
  3. Stage and commit this change:
git add version01.py
git commit -m "Added feature2.1 in development branch"
Added feature2.1

Step 3: Modify version01.py and Make the Second Commit

  1. Add the next required code changes in the file.
  2. Stage and commit this change:
git add version01.py
git commit -m "Added feature2.2 in development branch"
Added feature2.2

Step 4: Modify version01.py and Make the Third Commit

  1. Add the final required code changes in the file.
  2. Stage and commit this change:
git add version01.py
git commit -m "Feature2 completed"
feature2 completed

Step 5: Rebase Development Branch Commits onto the Production Branch

  1. Switch to the production branch (created from main):
git checkout -b production main

2. Rebase the commits from the development branch onto production:

git rebase development
Rebase verified

This will apply all the commits from the development branch onto production, ensuring the commit history is reflected in both branches.

Task-03: Cherry-picking a Specific Commit in the Production Branch

Step 1: Switch to the production Branch

  1. Ensure you are on the production branch:
git checkout production

Step 2: Cherry-pick the Specific Commit

  1. Identify the commit hash for the commit Added feature2.2 in development branch. You can find it using:
  2. Copy the hash of the commit with the message Added feature2.2 in development branch.
  3. Cherry-pick this commit:
git cherry-pick <commit-hash>

Step 3: Modify version01.py for the Cherry-picked Commit

  1. Open version01.py and add the required additional code after the cherry-picked content.
  2. Stage and commit the changes:
git add version01.py
git commit --amend -m "Optimized the feature"
changes commited

This approach applies the required code changes in both the development and production branches while maintaining a clean commit history.

git commit — amend allows you to modify the most recent commit by updating its content or commit message. It replaces the last commit with a new one that includes any new changes you’ve staged or fixes to the commit message. Use this to correct or enhance the latest commit before pushing it.

--

--

Muzammil Jan

Software Engineering student at Dha Suffa University, Karachi. Exploring the world of DevOps & Cloud! Love learning & giving back to open source communities.