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
Open your terminal.
Create and switch to a new branch (e.g., feature-branch)
git checkout -b feature-branch
Step 2: Make Changes in the Python File
- 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
- After making changes, save them without committing:
git stash
This will stash your changes and revert the working directory to the previous commit.
Step 4: Switch to a Different Branch
- Switch to another branch (e.g., main):
git checkout main
Step 5: Make Changes and Commit Them
- 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
- Now bring back the changes you stashed earlier:
git stash pop
This will apply the stashed changes on top of the commits in the main branch.
Step 7: Review and Commit the Combined Changes
- 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()
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
- Ensure you are on the development branch:
git checkout development
Step 2: Modify version01.txt and Make the First Commit
Open version01.py.
Add the required code changes after the relevant section.
Stage and commit this change:
git add version01.py
git commit -m "Added feature2.1 in development branch"
Step 3: Modify version01.py and Make the Second Commit
Add the next required code changes in the file.
Stage and commit this change:
git add version01.py
git commit -m "Added feature2.2 in development branch"
Step 4: Modify version01.py and Make the Third Commit
Add the final required code changes in the file.
Stage and commit this change:
git add version01.py
git commit -m "Feature2 completed"
Step 5: Rebase Development Branch Commits onto the Production Branch
- 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
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
- Ensure you are on the production branch:
git checkout production
Step 2: Cherry-pick the Specific Commit
Identify the commit hash for the commit Added feature2.2 in development branch. You can find it using:
Copy the hash of the commit with the message Added feature2.2 in development branch.
Cherry-pick this commit:
git cherry-pick <commit-hash>
Step 3: Modify version01.py for the Cherry-picked Commit
Open version01.py and add the required additional code after the cherry-picked content.
Stage and commit the changes:
git add version01.py
git commit --amend -m "Optimized the feature"
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.