Managing and Merging Local Changes in Git: A Practical Guide

(Just a disclaimer: This blog was generated by ChatGPT, after a lot of prompting and helping it say the right things.)

Introduction: Handling multiple branches and managing changes in Git can initially seem daunting, especially when working with remote repositories like GitLab. However, with a systematic approach, managing your code becomes a breeze. In this post, we’ll walk through a scenario and provide a step-by-step guide to effectively manage and merge your local changes.

Scenario: Let’s consider a situation where you’ve made several changes in your local Git repository. You then checkout a specific branch, update some other file, and commit that specific file. Online on GitLab, you merge that branch back into the main branch. Now, you want to check the other files that you changed into the main branch on your local Git repository. How would you go about doing this? Here’s a breakdown of the steps to follow:

  1. Stash Current Work (Optional):
    • If there are uncommitted changes in your working directory that you aren’t ready to commit yet, you might want to stash them to have a clean working area. Execute the command:
      git stash
      
  2. Switch to the Main Branch:
    • Ensure you are on the main branch by executing the command:
      git checkout main
      
  3. Update Your Local Main Branch:
    • Fetch the latest changes from the remote repository and merge them into your local main branch to ensure it is up-to-date with the merged changes from GitLab.
      git pull origin main
      
  4. Recover Your Stashed Changes (Optional):
    • If you stashed changes earlier, now is the time to reapply them using the command:
      git stash pop
      
  5. Create a New Branch (Optional):
    • It’s often good practice to create a new branch for your changes to keep the main branch clean and your changes organized.
      git checkout -b new-branch-name
      
  6. Stage Your Changes:
    • Stage the files you want to commit using the command:
      git add file1 file2 ...
      
  7. Commit Your Changes:
    • Commit your staged changes with a meaningful commit message using the command:
      git commit -m "Your commit message"
      
  8. Push Your New Branch to the Remote Repository:
    • Push your new branch (with your commit) to the remote repository using the command:
      git push origin new-branch-name
      
  9. Create a Merge Request:
    • Go to GitLab, and create a new Merge Request from new-branch-name into main. Review the changes, and once you are satisfied, complete the merge.
  10. Update Your Local Main Branch Again:
    • Once the Merge Request has been approved and merged in GitLab, switch back to your local main branch and pull the latest changes from the remote repository to ensure your local main branch is up-to-date.
      git checkout main
      git pull origin main
      

Conclusion: By following this sequence of steps, you can manage and merge your changes effectively in Git and GitLab. Keeping a clear history and organization is crucial for managing your repository efficiently, both for you and your team. Remember, practice makes perfect, so don’t hesitate to experiment and learn from each experience to become proficient in Git.

Updated: