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

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

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.

To use Git stash, you first create a new branch and make some changes to it. Then you can use the command git stash to save those changes. This will remove the changes from your working directory and record them in a new stash. You can apply these changes later. git stash list command shows the list of stashed changes.

You can also use git stash drop to delete a stash and git stash clear to delete all the stashes.

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.

To use git cherry-pick, you first create two new branches and make some commits to them. Then you use git cherry-pick <commit_hash> command to select the specific commits from one branch and apply them to the other.

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. git status command shows the files that have conflicts, git diff command shows the difference between the conflicting versions and git add command is used to add the resolved files.

Task-01:

  1. Create a new branch and make changes:

    COPY

    COPY

      git checkout -b feature-branch
      # Make changes to the code or add new files
    

Use git stash to save the changes without committing them:

  1. COPY

    COPY

      git stash save "Stashing changes for later"
    

Switch to a different branch, make changes, and commit:

  1. COPY

    COPY

      git checkout <other-branch>
      # Make changes to the code
      git add .
      git commit -m "Committing changes on the other branch"
    

Use git stash pop to bring the changes back and apply them on top of the new commits:

  1. COPY

    COPY

      git checkout feature-branch
      git stash pop
    

Task-02:

  1. Make changes in version01.txt:

    COPY

    COPY

      plaintextCopy codeThis is the bug fix in development branch
      After bug fixing, this is the new feature with minor alteration
      This is the advancement of the previous feature
      Feature 2 is completed and ready for release
    

Commit changes with messages:

  1. COPY

    COPY

      git add version01.txt
      git commit -m "Added feature2.1 in development branch"
    
      git add version01.txt
      git commit -m "Added feature2.2 in development branch"
    
      git add version01.txt
      git commit -m "Feature2 completed"
    

Reflect commit messages in Production branch using rebase:

  1. COPY

    COPY

      git checkout master  # switch to the master branch
      git pull origin master  # update master from the remote repository
      git rebase development  # rebase master onto development
      git push origin master  # push the changes back to the remote repository
    

Task-03:

Cherry-pick commit “Added feature2.2 in development branch” into the Production branch:

  1. COPY

    COPY

      git checkout master  # switch to the master branch
      git cherry-pick <commit-hash>  # use the commit hash from the development branch
    
  2. Add lines for optimization in the Production branch:

    COPY

    COPY

      plaintextCopy codeThis is the bug fix in development branch
      After bug fixing, this is the new feature with minor alteration
      This is the advancement of the previous feature
      Added few more changes to make it more optimized.
    
  3. Commit the optimized changes:

    COPY

    COPY

      git add version01.txt
      git commit -m "Optimized the feature"Day 11 Task: Advance Git & GitHub for D