Skip to main content

Command Palette

Search for a command to run...

Day 11 - Advance Git & GitHub for DevOps Engineers: Part-2

Updated
4 min readView as Markdown
Day 11 - Advance Git & GitHub for DevOps Engineers: Part-2
T

Hey there! 👋

I'm Tushar Ranjan, a DevOps Engineer passionate. Currently, on a learning adventure, I'm here to share my journey and Blogs in the world of cloud and DevOps.

🛠️ My focus? Making sense of AWS services, improving CI/CD, and diving into infrastructure as code. Whether you're fellow interns or curious enthusiasts, let's grow together in the vibrant DevOps space.

🌐 Connect with me for friendly chats, shared experiences, and learning moments. Here's to embracing the learning curve and thriving in the exciting world of AWS DevOps Technology!

Welcome back to our DevOps journey! In today’s post, I have explored more advanced Git concepts that are essential for DevOps engineers. I have covered Git Stash, Git Cherry-pick, and Resolving Conflicts. These tools and techniques will help you manage your code more efficiently and handle complex workflows with ease.

Git Stash

Git stash is a command that allows you to temporarily save changes you have made in your working directory without committing them. This is useful when you need to switch to a different branch to work on something else, but you don't want to commit the changes you've made in your current branch yet.

Using Git Stash

  1. Create a new branch and make some changes:
ubuntu@ip-172-31-10-117:~/test$ git checkout -b feature-branch
ubuntu@ip-172-31-10-117:~/test$ echo "Some changes" > file.txt
  1. Stash the changes:
ubuntu@ip-172-31-10-117:~/test$ git stash

This command will save your changes and clean your working directory.

  1. Switch to a different branch:
ubuntu@ip-172-31-10-117:~/test$ git checkout master
  1. Make some changes and commit them:
ubuntu@ip-172-31-10-117:~/test$ echo "Changes in master" > file-master.txt
ubuntu@ip-172-31-10-117:~/test$ git add file-master.txt
ubuntu@ip-172-31-10-117:~/test$ git commit -m "Changes in master branch"
  1. Apply the stashed changes:
ubuntu@ip-172-31-10-117:~/test$ git checkout feature-branch
ubuntu@ip-172-31-10-117:~/test$ git stash pop

This command will apply the stashed changes to your current branch and remove the stash from the stash list.

  1. Check the list of stashed changes:
ubuntu@ip-172-31-10-117:~/test$ git stash list
  1. Delete a specific stash or clear all stashes:
ubuntu@ip-172-31-10-117:~/test$ git stash drop
ubuntu@ip-172-31-10-117:~/test$ git stash clear

Cherry-pick

Git cherry-pick is a command that allows you to select specific commits from one branch and apply them to another. This can be useful when you want to selectively apply changes that were made in one branch to another.

Using Git Cherry-pick

  1. Create two new branches and make some commits:
ubuntu@ip-172-31-10-117:~/test$ git checkout -b branch1
ubuntu@ip-172-31-10-117:~/test$ echo "Feature in branch1" > file1.txt
ubuntu@ip-172-31-10-117:~/test$ git add file1.txt
ubuntu@ip-172-31-10-117:~/test$ git commit -m "Added feature in branch1"

ubuntu@ip-172-31-10-117:~/test$ git checkout -b branch2 master
ubuntu@ip-172-31-10-117:~/test$ echo "Feature in branch2" > file2.txt
ubuntu@ip-172-31-10-117:~/test$ git add file2.txt
ubuntu@ip-172-31-10-117:~/test$ git commit -m "Added feature in branch2"
  1. Cherry-pick a commit from branch1 to branch2:
ubuntu@ip-172-31-10-117:~/test$ git checkout branch2
ubuntu@ip-172-31-10-117:~/test$ git cherry-pick <commit_hash_from_branch1>

Replace <commit_hash_from_branch1> with the actual commit hash from branch1.

Resolving Conflicts

Conflicts can occur when you merge or rebase branches that have diverged, and you need to manually resolve the conflicts before Git can proceed with the merge/rebase.

Handling Conflicts

  1. Identify conflicts:
ubuntu@ip-172-31-10-117:~/test$ git status
  1. Show differences between conflicting versions:
ubuntu@ip-172-31-10-117:~/test$ git diff
  1. Resolve the conflicts manually and add the resolved files:
ubuntu@ip-172-31-10-117:~/test$ git add resolved_file.txt

Task 01

  1. Create a new branch and make some changes:
ubuntu@ip-172-31-10-117:~/test$ git checkout -b feature-branch
ubuntu@ip-172-31-10-117:~/test$ echo "Temporary changes" > temp-file.txt
  1. Stash the changes:
ubuntu@ip-172-31-10-117:~/test$ git stash
  1. Switch to a different branch and make some changes:
ubuntu@ip-172-31-10-117:~/test$ git checkout master
ubuntu@ip-172-31-10-117:~/test$ echo "Permanent changes in master" > master-file.txt
ubuntu@ip-172-31-10-117:~/test$ git add master-file.txt
ubuntu@ip-172-31-10-117:~/test$ git commit -m "Committed changes in master"
  1. Apply the stashed changes:
ubuntu@ip-172-31-10-117:~/test$ git checkout feature-branch
ubuntu@ip-172-31-10-117:~/test$ git stash pop

Task 02

  1. Add lines to version01.txt in the development branch:
ubuntu@ip-172-31-10-117:~/test$ git checkout development
ubuntu@ip-172-31-10-117:~/test$ echo "After bug fixing, this is the new feature with minor alteration" >> version01.txt
ubuntu@ip-172-31-10-117:~/test$ git add version01.txt
ubuntu@ip-172-31-10-117:~/test$ git commit -m "Added feature2.1 in development branch"

ubuntu@ip-172-31-10-117:~/test$ echo "This is the advancement of previous feature" >> version01.txt
ubuntu@ip-172-31-10-117:~/test$ git add version01.txt
ubuntu@ip-172-31-10-117:~/test$ git commit -m "Added feature2.2 in development branch"

ubuntu@ip-172-31-10-117:~/test$ echo "Feature 2 is completed and ready for release" >> version01.txt
ubuntu@ip-172-31-10-117:~/test$ git add version01.txt
ubuntu@ip-172-31-10-117:~/test$ git commit -m "Feature2 completed"
  1. Rebase these commits onto the production branch:
ubuntu@ip-172-31-10-117:~/test$ git checkout production
ubuntu@ip-172-31-10-117:~/test$ git rebase development

Task 03

  1. Cherry-pick the commit “Added feature2.2 in development branch” to the production branch:
ubuntu@ip-172-31-10-117:~/test$ git checkout production
ubuntu@ip-172-31-10-117:~/test$ git cherry-pick <commit_hash_of_feature2.2>
  1. Add additional lines and commit:
ubuntu@ip-172-31-10-117:~/test$ echo "Added few more changes to make it more optimized." >> version01.txt
ubuntu@ip-172-31-10-117:~/test$ git add version01.txt
ubuntu@ip-172-31-10-117:~/test$ git commit -m "Optimized the feature"

Conclusion

In this post, I have explored advanced Git commands such as Git Stash, Cherry-pick, and Resolving Conflicts. These commands are invaluable tools for DevOps engineers, enabling efficient code management and collaboration. By practicing these tasks, you will enhance your ability to handle complex Git workflows and maintain a clean and organized codebase.

~ Tushar Ranjan 😊

More from this blog

Tushar Ranjan's blog

72 posts