πŸ’©

programierds

Back to course
Lessons 2 / 3

Pull and push changes

πŸ” Key idea before we start

When you use Git, there are usually two copies of the project:

1️⃣ Your computer (local repository) 2️⃣ The internet (GitHub) (remote repository, usually on GitHub)

πŸ‘‰ Pull changes = bring what is on GitHub down to your PC πŸ‘‰ Push changes = send what you did on your PC up to GitHub


πŸ“₯ PULLING CHANGES (pull)

🧠 When do you need to pull changes?

  • When you start working

  • When someone else changed the project

  • When you work on multiple devices

  • To avoid conflicts

πŸ’‘ Golden rule: πŸ‘‰ Before working β†’ pull changes


πŸ”Ή Main command to pull changes

git pull

What does git pull do?

It does two things automatically:

  1. πŸ“₯ Downloads changes from the remote repository

  2. πŸ”€ Merges them with your local project


πŸ“Œ Real example (step by step)

Scenario:

  • Your project is on GitHub

  • A teammate changed a file

  • You want that change

Steps:

1️⃣ Go into the project folder:

cd my-project

2️⃣ Pull the changes:

git pull

3️⃣ Git responds with something like:

Updating a1b2c3d..e4f5g6h
1 file changed

πŸŽ‰ Done, you have the changes.


❗ Common error when pulling

If Git says something like:

error: Your local changes would be overwritten

πŸ‘‰ It means:

You have unsaved changes (not committed)

Solution:

Save your changes first:

git add .
git commit -m "Save changes before pulling"
git pull

πŸ“€ PUSHING CHANGES (push)

🧠 When do you need to push changes?

  • When you finished a task

  • When you want to back up your work

  • When others need your changes

πŸ’‘ Golden rule: πŸ‘‰ After working β†’ push changes


πŸ”Ή Full workflow to push changes

⚠️ This is VERY important, memorize it:

Edit β†’ add β†’ commit β†’ push

πŸ“Œ Full example of pushing changes

1️⃣ You modify a file

Edit:

hello.txt

2️⃣ Check status

git status

3️⃣ Stage changes

git add .

4️⃣ Save changes (commit)

git commit -m "Update greeting text"

πŸ‘‰ So far EVERYTHING IS LOCAL (only on your PC).


5️⃣ Push to GitHub

git push

πŸŽ‰ Now the change is on GitHub.


🌍 First time pushing (important)

The first time, Git needs to know where to push to.

You do it like this:

git push -u origin main

What does it mean?

  • origin β†’ remote repository (GitHub)

  • main β†’ main branch

  • -u β†’ remembers it for next time

After this, you just use:

git push

πŸ”„ REAL workflow cycle (real life)

In a real job you ALWAYS do this:

git pull
# work
git add .
git commit -m "Clear message"
git push

πŸ“Œ This cycle saves you from problems


⚠️ Conflicts when pulling (explained simply)

A conflict happens when:

  • You changed a line

  • Someone else changed the same line

Git doesn’t know which one to keep πŸ˜΅β€πŸ’«

Git will show you something like:

<<<<<<< HEAD
Your version
=======
Their version
>>>>>>> commit

Solution:

  1. Open the file

  2. Pick which version stays

  3. Delete the markers

  4. Save

  5. Run:

git add .
git commit -m "Resolve conflict"
git push

🧠 Essential commands (recap)

ActionCommand
See statusgit status
Pull changesgit pull
Stage changesgit add .
Save changesgit commit -m "message"
Push changesgit push

❌ Typical beginner mistakes

❌ Pushing without pulling first ❌ Not committing ❌ Messages like β€œchanges” ❌ Working directly on main in a team


🧠 Final rule (very important)

πŸ“₯ Before working β†’ git pull πŸ“€ After working β†’ git push