Intro to git

Block 3.2: Synchronizing Changes

Jan Simson

Syncing Changes: Overview

You first pull the latest changes from the remote and then push your own changes

Getting Changes: git pull

  • You can retrieve the latest changes from github by running git pull
  • This fetches the latest changes and merges them into your current branch
  • Typically your local branch is set up to track a remote branch with the same name (the upstream)
    • If not, you get a warning from git
# Pull from the tracked remote branch
git pull

# Pull from a specific branch
git pull <remote> <branch>

Getting Changes: git pull

  • This fetches the latest changes and merges them into your current branch
    • git pull really does two things here
  • You can also run git fetch to fetch the latest changes and then git merge <remote>/branch to merge them
  • git fetch will always fetch the latest changes without doing anything with them (which can be useful)

Pushing Your Changes: git push

  • You can send your changes to github by running git push
  • This only works if there are no new changes at the remote
    • If there have been changes since your last pull you need to pull again
# Push to the tracked remote branch
git push

# Push a branch to the remote for the first time
# -u is short for --set-upstream
git push -u <remote> <branch>

Questions?

Practical: Adding your own repository to Github

  1. Create a new repository on GitHub with the name git-intro-example
    • Make sure NOT to add a README or any other file when creating it
  2. In the example git repository we’ve been working in so far, add the just created GitHub repo as a remote
    • git remote add origin <URL>
  3. Push you changes so far to GitHub
    • git push -u origin main

Optional Practical: Collaboration

  1. Form pairs
  2. One person adds the other to their git-intro-example repository
    • Via the GitHub UI (write privileges)
  3. Each, add a new entry to the library.txt
  4. Try to provoke a merge conflict

End of Section 🎉

Any Questions?

[🏡 Back to Overview]

[⏩️ Next Section]