100===Dev Ops/Git

How do you sync your local repository with a remote one with git?

블로글러 2024. 6. 9. 13:19

To sync your local repository with a remote one using Git, you'll perform a series of steps that include fetching changes, merging updates, and possibly resolving conflicts, akin to updating your personal copy of a shared document with the latest edits made by others.

The Big Picture

Imagine you and your friends are collaboratively writing a book. You have your own copy of the book, and there’s a central copy that everyone updates. To make sure your copy has all the latest changes, you periodically need to synchronize your copy with the central one. This is what syncing your local repository with a remote one in Git is like.

Core Concepts

  1. Remote Repository: The central place where all your team’s code resides.
  2. Local Repository: Your own copy of the project on your computer.
  3. Fetching: Pulling down the latest changes from the remote repository to your local repository.
  4. Merging: Integrating those changes into your local working copy.
  5. Pulling: A combination of fetching and merging.
  6. Pushing: Sending your changes from your local repository to the remote repository.

Detailed Walkthrough

  1. Fetch the latest changes from the remote repository:

    • Command: git fetch origin

    • This command fetches all the changes from the remote repository (commonly named origin) without merging them into your working copy. It updates your remote-tracking branches.

      git fetch origin
  2. Merge the changes into your local branch:

    • Command: git merge origin/main (or whatever the main branch is called)

    • This merges the fetched changes into your current branch.

      git merge origin/main

1,2. Pull the latest changes from the remote repository:
- Command: git pull
- This command is essentially a shortcut that combines git fetch and git merge in one step.

```bash
git pull
```
  1. Resolve any conflicts:

    • If there are changes in the same parts of files that you have also modified, Git will prompt you to resolve these conflicts manually.
  2. Push your changes to the remote repository:

    • Command: git push

    • Once your local copy is up-to-date and you’ve made your changes, you can push them to the remote repository.

      git push origin main

Understanding Through an Example

Imagine you and a friend are both working on a document. Your friend makes changes and updates the shared copy (remote repository). To get these changes:

  1. Fetch: You get a notification of all changes your friend made (without applying them to your document yet).
  2. Merge: You apply these changes to your copy of the document.
    1,2. Pull: You do both steps in one go.
  3. Resolve conflicts: If you both made changes to the same paragraph, you discuss and decide whose changes to keep.
  4. Push: Once you’ve made your edits, you update the shared copy with your new changes.

Conclusion and Summary

Syncing your local repository with a remote one involves fetching updates from the remote repository, merging those updates into your local repository, and resolving any conflicts that arise. The key commands are git fetch, git merge, git pull, and git push.

Test Your Understanding

  1. What is the difference between git fetch and git pull?
  2. How do you resolve conflicts that arise during a merge?
  3. What command would you use to send your local changes to the remote repository?

Reference

For more detailed information, you can check the Git documentation.

728x90

'100===Dev Ops > Git' 카테고리의 다른 글

Git 제대로 이해하기: 시간여행자의 코드 관리 비법 🚀  (2) 2024.11.17
Gitlab Introduced  (0) 2024.06.11
Git Introduced  (0) 2024.05.26