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

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:**
    

```bash
ubuntu@ip-172-31-10-117:~/test$ git checkout -b feature-branch
ubuntu@ip-172-31-10-117:~/test$ echo "Some changes" > file.txt
```

2. **Stash the changes:**
    

```bash
ubuntu@ip-172-31-10-117:~/test$ git stash
```

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

3. **Switch to a different branch:**
    

```bash
ubuntu@ip-172-31-10-117:~/test$ git checkout master
```

4. **Make some changes and commit them:**
    

```bash
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"
```

5. **Apply the stashed changes:**
    

```bash
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.

6. **Check the list of stashed changes:**
    

```bash
ubuntu@ip-172-31-10-117:~/test$ git stash list
```

7. **Delete a specific stash or clear all stashes:**
    

```bash
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:**
    

```bash
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"
```

2. **Cherry-pick a commit from branch1 to branch2:**
    

```bash
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:**
    

```bash
ubuntu@ip-172-31-10-117:~/test$ git status
```

2. **Show differences between conflicting versions:**
    

```bash
ubuntu@ip-172-31-10-117:~/test$ git diff
```

3. **Resolve the conflicts manually and add the resolved files:**
    

```bash
ubuntu@ip-172-31-10-117:~/test$ git add resolved_file.txt
```

## Task 01

1. **Create a new branch and make some changes:**
    

```bash
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
```

2. **Stash the changes:**
    

```bash
ubuntu@ip-172-31-10-117:~/test$ git stash
```

3. **Switch to a different branch and make some changes:**
    

```bash
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"
```

4. **Apply the stashed changes:**
    

```bash
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:
    

```bash
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"
```

2. **Rebase these commits onto the production branch:**
    

```bash
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:**
    

```bash
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>
```

2. **Add additional lines and commit:**
    

```bash
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 😊
