Skip to content

Instantly share code, notes, and snippets.

@edoves
Created November 25, 2024 06:39
Show Gist options
  • Save edoves/19fcfc0dcfc37c964763865487feef7f to your computer and use it in GitHub Desktop.
Save edoves/19fcfc0dcfc37c964763865487feef7f to your computer and use it in GitHub Desktop.

Git Notes

  • git fetch

git fetch

git fetch is a Git command that retrieves changes from a remote repository to your local repository but does not merge them into your local working branch. It allows you to see what others have added or changed in the remote repository without affecting your current local codebase.

What does it do?

  1. Updates your local view of the remote repository: It fetches the latest commits, branches, and tags from the remote repository.
  2. Does not merge changes: Unlike git pull, it does not automatically merge changes into your current branch. This gives you control over when and how to integrate the changes.
  3. Keeps your local repository up-to-date: After fetching, you can inspect the fetched changes before deciding to merge or rebase.

How it works

When you run git fetch, Git retrieves the data from the remote repository and updates the remote-tracking branches (e.g., origin/main) in your local repository. You can then review these changes and decide what to do next (e.g., merge them into your branch).

Example Usage

1. Basic git fetch

git fetch
  • This command fetches all branches and updates the remote-tracking branches in your local repository.

2. Fetch from a specific remote

git fetch origin
  • Here, origin is the name of the remote repository (default name for the primary remote).

3. Fetch a specific branch

git fetch origin feature-branch
  • This fetches the feature-branch from the remote origin and updates your local tracking information for that branch.

4. Inspect the fetched changes

After fetching, you can inspect the fetched changes using:

git log origin/main
  • This displays the commits on the origin/main branch (the main branch on the remote repository).

5. Merging the fetched changes

To integrate the fetched changes into your local branch, you can either:

  • Merge: git merge origin/main
  • Rebase: git rebase origin/main

Practical Scenario

  1. Assume you're collaborating on a project.
  2. Your teammate has pushed some changes to the main branch in the remote repository.
  3. You want to see their updates without merging them into your local main branch immediately.

Commands:

git fetch origin
git log origin/main
  • This fetches the updates and allows you to review their commits before integrating them.

By using git fetch, you ensure that your workflow is non-disruptive and provides greater control over incorporating remote changes.

--

I use this git merge origin/main but I do have conflicts what to do next?

When you encounter conflicts during a git merge, it means Git is unable to automatically reconcile changes between your branch and the branch you are merging (e.g., origin/main). Here’s how to resolve conflicts step-by-step:


Step 1: Understand the Conflict

After the git merge origin/main command, Git will pause the merge and display a message about the conflict:

CONFLICT (content): Merge conflict in <file>
Automatic merge failed; fix conflicts and then commit the result.
  • Git marks the conflicting files as unmerged.
  • The files with conflicts will include special markers (<<<<<<<, =======, >>>>>>>) to indicate the conflicting changes.

Step 2: Check the Files with Conflicts

Use the following command to list the files with conflicts:

git status
  • Look for files under the "both modified" category. These are the ones with merge conflicts.

Step 3: Open the Conflicting Files

Open each conflicting file in a text editor or IDE. You will see conflict markers like this:

<<<<<<< HEAD
Your changes (local branch)
=======
Changes from origin/main (remote branch)
>>>>>>> origin/main
  • The section between <<<<<<< HEAD and ======= is your local changes.
  • The section between ======= and >>>>>>> origin/main is the remote branch's changes.

Step 4: Resolve the Conflicts

Decide how to handle the conflicting changes:

  1. Keep your changes.
  2. Keep the remote changes.
  3. Combine both changes.

Example: If you want to combine the changes, edit the file to look like this:

Resolved content here.

Step 5: Mark the Conflict as Resolved

Once you've resolved a conflict in a file, mark it as resolved by staging it:

git add <file>

Repeat this for all conflicting files.


Step 6: Complete the Merge

After resolving all conflicts and staging the files, complete the merge with:

git commit

Git will use the default merge message, but you can edit it if necessary.


Step 7: Verify the Merge

Check the merge status:

git log --oneline

Ensure the merge commit appears at the top of your commit history.


Optional: Abort the Merge (If Needed)

If resolving conflicts feels too complex or you want to restart the process:

git merge --abort

This will undo the merge and return your branch to its previous state.


Tips for Avoiding Future Conflicts

  1. Pull regularly: Frequently pull changes from the remote repository (git pull) to keep your branch up-to-date.
  2. Small commits: Make smaller, incremental commits to minimize overlapping changes.
  3. Collaborate effectively: Communicate with teammates to avoid editing the same sections of code simultaneously.

If conflicts persist or the resolution is unclear, let me know, and I can help further!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment