Mastering Git: Practical Scenarios for Effective Version Control

Mastering Git: Practical Scenarios for Effective Version Control

Explore some problem-based scenarios that senior DevOps/Linux engineers might encounter,

In this article we will explore some problem-based scenarios that senior DevOps/Linux engineers might encounter, along with the corresponding Git commands to address these issues

Scenario: Changing the Remote URL

Check the Current Remote URL

#Check Current Remote URL
git remote -v
#Change Remote URL
git remote set-url origin new_remote_url
#Verify Changes
git remote -v
#Fetch to Ensure Connectivity
git fetch

Explanation: Update the URL of the remote repository. Replace URL with the URL of your remote.

Scenario: The branch is Behind the Master and Needs an Update


git stash
git pull origin master
git stash apply

Explanation: Stash local changes, pull updates from the master branch, and reapply the stashed changes.

Scenario: Merge Conflict During Pull Request


git pull origin master
# resolve conflicts
git add .
git commit -m "Merge branch 'master' into feature_branch"
git push origin feature_branch

Explanation: Pull changes from the master branch, resolve conflicts, and push the updated feature branch.

Scenario: Accidentally Deleted a Local Branch

git reflog
git checkout -b new_branch_name commit_hash

Explanation: Recover the deleted branch using git reflog to find the commit hash before deletion and then create a new branch.

Scenario: Squashing Commits Before Pull Request

git rebase -i HEAD~n

Explanation Interactively rebase the last 'n' commits to squash them into a single commit before submitting a pull request.

Scenario: Accidentally Pushed to the Wrong Branch

git reflog
git reset --hard commit_hash
git push --force origin correct_branch

Explanation: Identify the commit hash from the reflog and reset the branch, then force push the changes to the correct branch.

Scenario: Discarding Local Changes

git checkout -- file_path

Explanation: Discard changes in a specific file by checking it out from the last committed version.

Scenario: Need to Revert to a Previous Commit

git log
git revert commit_hash
git push origin branch_name

Explanation: Identify the commit to revert using git log, create a new commit that undoes the specified commit, and push the changes.

Scenario: Large File Pushed to Repository

git filter-branch --tree-filter 'rm -rf path/to/large_file' HEAD
git push --force origin branch_name

Explanation: Use git filter-branch to remove the large file from the entire commit history, then force-push the changes.

Scenario: Accidentally Committed to the Master Branch

git branch temp_branch
git reset --hard origin/master
git checkout -b feature_branch

Explanation: Create a temporary branch, reset the local master branch to the remote master, and create a new feature branch.

Check whose profile is configured

git config --global
git config --global

These commands will output the corresponding configuration values if they are set. If the output is empty, it means that the specific configuration has not been set, and we can set with the below commands

git config --global
git config --global

Did you find this article valuable?

Support Muhammad Usama by becoming a sponsor. Any amount is appreciated!