Git Workflow Overview
Git provides a powerful version control system to track changes, collaborate on projects, and integrate code seamlessly. The workflow is divided into local machine actions and GitHub repository interactions. The workflow comprised of the followings steps
Step 1. Push Changes Regularly
- Stage changes:
- Commit changes:
- Push to remote:
git add .git commit -m "Fix user profile bug"git pushStep 2. Push a New Branch
- Create a new branch:
- Push and set upstream:
git checkout -b feature-logingit push -u origin feature-loginStep 3. Push Tags
- Create a tag:
- Push the tag:
git tag -a v1.0 -m "Release v1.0"git push origin v1.0Step 4. Prune Remote Branches
- Remove deleted branches from remote:
git push origin --prune
Best Practices and Explanations
Best Practice | Command/Tip | Description | Example |
Pull Before Pushing | git pull | Always pull changes before pushing to avoid conflicts. | git pull origin main |
Use Descriptive Commit Messages | git commit -m "Message" | Write clear messages to maintain good collaboration and clarity. | git commit -m "Fix login issue" |
Tag Important Milestones | git tag -a <tag_name> -m "Message" | Use tags to mark important points (e.g., releases). | git tag -a v1.0 -m "Version 1.0" |
git push origin <tag_name> | Push tags to the remote. | git push origin v1.0 | |
Avoid Force Pushing | Use git push --forcesparingly | Use force push only when rebasing or undoing commits is necessary. | git push --forceafter git rebase |
Regularly Sync Changes | git push | Push changes frequently to avoid losing work and maintain collaboration. | git push |
Setting Up Git
Global-level.gitignore
Project-Specific .gitignore
Quick Git
git push
git pull
Step in Repo
- Fork the Repo
- Clone the repo
- create a branch for the repo:
repo-features
Your local branch has diverged from the remote branch, meaning there are changes on both ends that need to be reconciled. You have a few options to resolve this:
1️⃣ Merge (Preserve both local and remote changes)
If you want to merge the changes from the remote branch into your local branch without rebasing:
bash
CopyEdit
git pull --no-rebase
OR set it permanently:
bash
CopyEdit
git config pull.rebase false
git pull
2️⃣ Rebase (Apply your changes on top of remote changes)
If you want to rebase your local changes on top of the remote branch:
bash
CopyEdit
git pull --rebase
OR set it permanently:
bash
CopyEdit
git config pull.rebase true
git pull
This keeps a cleaner commit history but may require resolving conflicts interactively.
3️⃣ Fast-Forward Only (If you have no local changes)
If you want to only fast-forward when there are no conflicts:
bash
CopyEdit
git pull --ff-only
If this fails, it means you have local changes that must be merged or rebased.
4️⃣ Manually Resolve Divergence (If unsure)
If you’re not sure which approach to take:
bash
CopyEdit
git fetch origin
git status
Check what has changed, then either merge (git merge origin/main) or rebase (git rebase origin/main) accordingly.
If you run into conflicts during git pull --rebase:
- Run
git statusto see conflicted files. - Manually resolve conflicts in the affected files.
- Run:ORif merging.
bash
CopyEdit
git add .
git rebase --continue
bash
CopyEdit
git merge --continue
Choose the method based on whether you prefer to preserve history (merge) or keep it linear (rebase). Let me know if you need help resolving conflicts! 🚀
4o
Here is the simplest way to understand it, like a teacher explaining step by step.
How to work with a downloaded repo and create your own branch safely
When you download or clone a repository from online, you usually have two goals:
- Work on your own copy without other people’s updates disturbing your work.
- Still receive updates from the original repo whenever they release changes.
To achieve this, you need two things:
- Your own branch (where you do your work).
- A connection to the original repo (so you can pull new updates whenever they happen).
Let us break it down gently.
Step 1: Clone the repository
If you have not already cloned it, do:
git clone https://github.com/owner/repo.git
cd repo
This gives you a local copy.
Step 2: Create your own branch
You do not want to work directly on main. Create your own branch:
git checkout -b teslim-branch
Now all the work you do is inside your branch, not the main branch.
This protects your work from any updates happening in the original repo.
Step 3: Add the original repo as “upstream”
Right now, your repo only knows about your copy of the project on GitHub (called origin).
But it does not know where the original code came from.
You must add the main repo as a second remote called upstream:
git remote add upstream https://github.com/owner/repo.git
You can confirm it with:
git remote -v
You should now see:
- origin → your GitHub copy
- upstream → the original author’s repo
Step 4: When the original repo updates, you can pull the changes
If the original repo gets new updates, you can bring those updates into your own project.
First, switch to main:
git checkout main
Fetch updates from the original repo:
git fetch upstream
Merge them into your main:
git merge upstream/main
Now your local main is fully updated.
Step 5: Bring the updated main into your own branch
Switch back to your own branch:
git checkout teslim-branch
Merge the new main into your branch:
git merge main
This updates your branch with the new changes without losing your own work.
In simple teacher language
Think of it like this:
- main is the original textbook.
- upstream is the school library where the original textbook is updated.
- your branch is your personal notebook.
When the school updates the textbook:
- You collect a fresh copy from the library (fetch + merge upstream into main).
- You update your personal notebook using the new materials (merge main into your branch).
Your work remains safe.
Nothing overwrites your notes.
You still get the latest updates whenever you want.
If you want, I can write the exact commands for you using your specific repo link.
You said:
instead of this generic repo.. use this git@github.com:microsoft/generative-ai-for-beginners.git, and rewrite this from the begining with guide
ChatGPT said:
Below is a clean, simple, teacher-style explanation using your actual repository:
git@github.com:microsoft/generative-ai-for-beginners.git
I will walk you through everything from the beginning.
How to Work Safely With the “Generative AI for Beginners” Repo
Your goals:
- Create your own branch so your work is not affected by updates from Microsoft
- Still receive new updates from the main Microsoft repo
- Bring those updates into your own branch whenever you choose
Let us go step by step in simple terms.
Step 1: Clone the Microsoft repository
Open your terminal and run:
git clone git@github.com:microsoft/generative-ai-for-beginners.git
cd generative-ai-for-beginners
Now you have a local copy of the entire course.
Step 2: Create your own working branch
You should never work directly on main.
Create your personal branch:
git checkout -b teslim-branch
Now all your edits, notes, experiments, exercises, and updates happen in teslim-branch.
Your work is protected from changes in the main Microsoft repo.
Step 3: Add the original Microsoft repo as “upstream”
Right now your repo only knows about origin, which points to your own copy on GitHub.
But it does not know where the code originally came from.
So you must connect it to Microsoft’s source repo (the one you cloned):
git remote add upstream git@github.com:microsoft/generative-ai-for-beginners.git
Check it:
git remote -v
You should see:
- origin → your GitHub repo
- upstream → Microsoft’s original repo
Step 4: When Microsoft updates their repo, bring updates into your main
Any time Microsoft releases new lessons or fixes, you can update your local copy.
First switch back to main:
git checkout main
Fetch new updates from Microsoft:
git fetch upstream
Merge them into your main branch:
git merge upstream/main
Now your own main branch matches Microsoft’s latest version.
Step 5: Update your work branch
Switch back to your working branch:
git checkout teslim-branch
Bring the latest updates into your branch:
git merge main
This updates your branch but keeps all your personal work intact.
A very simple analogy
Think of it like this:
- “Microsoft’s repo” is the master textbook in the library.
- “main” is your personal clean copy of the textbook.
- “teslim-branch” is your notebook where you write and experiment.
When Microsoft updates the textbook:
- You fetch the latest version into your clean copy (main).
- You update your notebook with the new material (merge main into teslim-branch).
You always stay up to date
Your work is never affected
You stay in full control
If you want, I can write this as a ready-to-save study note or produce the exact GitHub Desktop steps instead of terminal commands.
You said:
Where does pull comes in here
ChatGPT said:
Here is the simplest way to understand where git pull fits, using the same Microsoft repo example and teacher-style explanation.
First, remember what git pull actually does
git pull = git fetch + git merge
It brings updates from a remote branch into your current branch.
But which remote you pull from matters:
git pull origin mainpulls from your GitHub copygit pull upstream mainpulls from Microsoft’s original repo
So now let us place git pull inside the workflow you are trying to achieve.
Where git pull fits in your workflow
You have three players:
- Microsoft repo → upstream
- Your repo on GitHub → origin
- Your local computer → branches (main, teslim-branch)
Two situations where you will use git pull
Situation 1: You want the latest updates from Microsoft
This is the most important one for you.
Use:
git checkout main
git pull upstream main
This does two things:
- It fetches the newest updates from Microsoft
- It merges them into your local main
This replaces the earlier two-step version:
git fetch upstream
git merge upstream/main
Both methods are correct. git pull upstream main is just faster.
Then you update your working branch:
git checkout teslim-branch
git merge main
Situation 2: You want to update your own GitHub repo (origin)
If you want to sync your local main back to your own GitHub account:
git checkout main
git pull origin main # gets any updates from your own GitHub copy
git push origin main # sends your updated main to GitHub
This keeps your GitHub fork up to date as well.
Putting it all together (full workflow with pull)
Here is the complete, simple picture using git pull.
Step 1: You hear that Microsoft updated the course
You update your local main:
git checkout main
git pull upstream main
Now your local main is fresh.
Step 2: Update your working branch
git checkout teslim-branch
git merge main
Your branch now has Microsoft’s updates plus your own work.
Step 3: Update your own GitHub fork (optional)
git checkout main
git push origin main
This pushes everything from your local main back to your GitHub repo.
Easy analogy
git pull upstream mainis like going to the library and getting the newest version of the Microsoft textbook.git merge mainis like updating your own notebook with the new content.git push origin mainis like updating the copy of your notebook stored online.