67db91dd-3847-4b3f-9b8d-392.../docs/06-pull-push-diff.md

1.5 KiB

git pull & push & diff

Goal

Work with a remote, push and pull changes, and compare differences.

Set Up a Local "Remote" (Offline-Friendly)

We'll simulate a remote using a bare repository.

cd ~/lab-git-essentials
git init --bare remote.git

Connect Your Repo to the Remote

cd ~/lab-git-essentials/my-first-repo
git remote add origin file://$HOME/lab-git-essentials/remote.git
git remote -v

Push Your Main Branch

git checkout main
git push -u origin main

Simulate a Collaborator and Pull

Create a second local clone, make a change, push it, then pull it back.

cd ~/lab-git-essentials
git clone file://$HOME/lab-git-essentials/remote.git collaborator
cd collaborator
echo "Edit from collaborator" >> collab.txt
git add collab.txt
git commit -m "Collaborator adds a file"
git push origin HEAD:main

# Back in your original repo, pull the change
cd ~/lab-git-essentials/my-first-repo
git pull
ls

View Differences

# Compare working tree vs last commit
echo "temp edit" >> notes.txt
git diff

# Compare staged vs last commit
git add notes.txt
git diff --staged

# Compare two commits (use your own SHAs)
git log --oneline -n 2
git diff HEAD~1 HEAD

(Optional) Use a Hosted Remote

If you have GitHub/GitLab:

git remote set-url origin https://github.com/<you>/<repo>.git
git push -u origin main
git pull --rebase

Outcome

You can push to and pull from a remote, and use git diff to compare changes.