2 min read
Git Workflow Cheat Sheet
git
This repository contains a quick reference guide for common, yet specific, Git scenarios.
Update a Forked Repository From the Original
Scenario: You forked a repository, cloned it locally, and now you want to update your local version with the latest changes from the original (upstream) repository.
Commands:
# Add the original repository as an upstream remote (Do this only once)
git remote add upstream REPO_URL
# Fetch the latest changes from the original repository
git fetch upstream
# Switch to your local main branch
git checkout main
# Merge the changes from the original main branch into your local main branch
git merge upstream/main
# Push the updated local main branch to your forked repository on GitHub
git push origin mainReset a Local Branch to Match the Remote Version
Scenario: You made updates or changes to a local branch and want to discard them completely, reverting the branch to look exactly like the version on GitHub (remote).
Commands:
# Fetch the latest state of the remote branches
git fetch origin
# Switch to the branch you want to reset (replace <branch> with your branch name)
git checkout <branch>
# Force reset your local branch to exactly match the remote branch
git reset --hard origin/<branch>Bring a Specific File From Another Branch Without Merging
Scenario: You are on the main branch and you want to pull a specific file from another branch (e.g., V2P) without merging the entire branch.
Commands:
# Ensure you are on the main branch (you can use either checkout or switch)
git checkout main
# OR
git switch main
# Make sure your main branch is up to date
git pull origin main
# --- Bring the file from the other branch (Choose ONE of the two methods below) ---
# Method A: Using checkout
git checkout V2P -- path/to/your_file.py
# Method B: Using restore (Newer, recommended approach)
git restore --source=V2P path/to/your_file.py
# Add the file to the staging area
git add path/to/your_file.py
# Commit the new file to your current branch
git commit -m "Bring finished file from V2P"
# Push the changes to GitHub
git push origin main
Comments
Loading the discussion…