Advance Git & GitHub for DevOps Engineers Part-2

Advance Git & GitHub for DevOps Engineers Part-2

Day 11 of 90daysofdevops

Git Stash

  • Git stash is a command in the Git version control system that allows a developer to temporarily save and stash away their current changes, without committing them to the Git repository.

  • This is useful when a developer is working on a particular branch, but needs to switch to another branch or make changes to a different part of the codebase without committing their current changes.

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

# Saving the work without commit
git stash

# Reapplying Stashed Changes
git stash pop

# Saving Stashes with the message
git stash save "<Stashing Message>"  

# Check the Stored Stashes
git stash list 

# Delete a stash from the queue
git stash drop  

# Deleting all the available stashes at once
git stash clear  

# Re-apply the changes that you just stashed by using the git stash
git stash apply  

# Track the stashes and their changes
git stash show

Cherry-pick

  • "Cherry-pick" is a feature in Git that allows developers to apply a specific commit from one branch to another branch.

  • This can be useful when a developer needs to include a particular change or feature from one branch into another branch that has diverged from the original branch. For example, if a bug fix was made on a development branch, but that fix is also needed on a release branch, a developer can use cherry-pick to apply that fix to the release branch without merging the entire development branch.

git cherry-pick <commit_id>

Resolving Conflicts

  • In Git, conflicts can arise when two or more developers make changes to the same file(s) and attempt to merge their changes together. Git is designed to detect and highlight such conflicts, and it is up to the developers to resolve them manually.

  • Resolving conflicts in Git involves identifying the conflicting changes and deciding how to integrate them into a new, merged version of the file. This can be done using Git's built-in merge tools or manually by editing the conflicting sections of the file.

Task 1:

  1. Create a new branch and make some changes to it.

     git checkout -b stash-b
     git branch
     vim version01.txt
     cat version01.txt
     git add .
    

  2. Use git stash to save the changes without committing them.

     git add .
    
     git stash
     git status
    
  3. Switch to a different branch, make some changes and commit them.

     git checkout dev
     vim version01.txt
     cat version01.txt
     git add .
     git commit -m "version file commited"
    

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

     git stash list
     git stash pop
     git add .
     git commit -m "git stash file commited"
     git log --oneline
    

Task 2:

  1. In version01.txt of the development branch add the below lines after "This is the bug fix in development branch" that you added in Day10 and reverted to this commit.

      git checkout -b prod
      git branch
      git checkout dev
      vim version01.txt  :- "This is the bug fix in development branch"
      cat version01.txt 
      git add version01.txt 
      git commit -m "commited file on dev"
    

  2. Line2>> After bug fixing, this is the new feature with minor alteration

    Commit this with the message "Added feature2.1 in development branch"

     vim version01.txt :- "After bug fixing, this is the new feature with minor alteration"
     cat version01.txt 
     git add version01.txt 
     git commit -m "Added feature 2.1 in development branch"
    
  3. Line3>> This is the advancement of previous feature

    Commit this with the message "Added feature2.2 in development branch"

     vim version01.txt  :- "This is the advancement of  previous feature "
     cat version01.txt 
     git add version01.txt 
     git commit -m "Added feature 2.2 in development branch"
    

  4. Line4>> Feature 2 is completed and ready for release

    Commit this with the message "Feature2 completed"

     vim version01.txt  :- "Feature 2 is completed and ready for release"
     cat version01.txt 
     git add version01.txt 
     git commit -m "Feature2 completed"
    

  5. All these commits messages should be reflected in the Production branch too which will come out from the Master branch (Hint: try rebase).

     git branch
     git checkout prod
     git rebase dev
     git log --oneline
    

Task 3:

  1. In Production branch Cherry pick Commit “Added feature2.2 in development branch” and added below lines in it:

     git log --oneline
     git cherry-pick <commit_id>
     vi version01.txt  #remove all the head and arrow mark as well as code that not require
    

  2. Line to be added after Line3>> This is the advancement of previous feature

     vi version01.txt :- "This is the advancement of previous feature"
     git add version01.txt
     git commit -m "adv feature"
    
  3. Line4>>Added few more changes to make it more optimized. Commit: Optimized the feature

     vi version01.txt  :- "Added few more changes to make it more optimized"
     git add version01.txt
     git commit -m "Optimized the feature"
     git log --oneline
    

Thank You,

Abhisek Moharana