DEV Community

Cover image for Managing Git Repositories: Resolving Branch, Remote, and Commit Issues in a Vite React Project
TD!
TD!

Posted on

Managing Git Repositories: Resolving Branch, Remote, and Commit Issues in a Vite React Project

This report provides a detailed guide on managing common Git issues related to branches, remotes, and commit history when working on a Vite React project. In this case, I encountered a series of challenges, including removing unwanted collaborators, setting up a new branch from a cloned repository, and clearing commit history. This guide explains the commands used, the concepts behind them, and the technical steps taken to resolve these issues.

This is Day #16 of the #100daysofMiva coding challenge. and a continuation of my previous post (Day #15) where I started building my personal website.

Report: Managing Git Repositories: Resolving Branch, Remote, and Commit Issues in a Vite React Project
Introduction
This report provides a detailed guide on managing common Git issues related to branches, remotes, and commit history when working on a Vite React project. In this case, we encountered a series of challenges, including removing unwanted collaborators, setting up a new branch from a cloned repository, and clearing commit history. This guide explains the commands used, the concepts behind them, and the technical steps taken to resolve these issues.

1. Cloning the Project and Branch Setup

Initially, the project was cloned from an existing GitHub repository. However, we needed to remove any association with the original owner and start afresh under a new main branch.

git branch -vv
git checkout -b main origin/main
Enter fullscreen mode Exit fullscreen mode

Explanation:

git branch -vv shows the list of branches and tracks the upstream branches for each one. We used this to check the status of our branches.

git checkout -b main origin/main attempts to create a new main branch locally, tracking origin/main. However, the origin/main branch didn't exist, causing an error.

Solution:

To set up the main branch correctly:

git checkout -b main
Enter fullscreen mode Exit fullscreen mode

2. Deleting an Unwanted Branch

Next, we needed to delete the old start-engine branch, both locally and remotely, to completely remove its traces.

Commands Used:


git branch -d start-engine
git push origin --delete start-engine
Enter fullscreen mode Exit fullscreen mode

Explanation:

git branch -d branch-name deletes the local branch-name branch.
git push origin --delete branch-name deletes the remote start-engine branch from GitHub.

3. Clearing Commit History

To dissociate the project from the original commit history (which could include the original owner's contributions), we decided to clear the commit history and start afresh.

Commands Used:


git checkout --orphan new-main
git commit -m "First commit"
git branch -D main
git branch -m new-main main
git push --force origin main
Enter fullscreen mode Exit fullscreen mode

Explanation:

git checkout --orphan new-main creates a new branch new-main without any commit history.

git commit -m "First commit" creates the first commit for this new branch.
git branch -D main deletes the old main branch.
git branch -m new-main main renames the new-main branch to main.
git push --force origin main force-pushes the new main branch to the remote, overwriting the old commit history.

This method resets the commit history while retaining the project files.

4. Handling Upstream and Remote Settings

After clearing the commit history, we needed to ensure that the new main branch was correctly associated with the GitHub repository.

Commands Used:


git remote -v
git push --set-upstream origin main
Enter fullscreen mode Exit fullscreen mode

Explanation:

git remote -v displays the current remotes associated with the repository.

git push --set-upstream origin main sets the main branch to track the origin/main branch, making it easier to push and pull in the future.

5. Resolving Missing Package Errors in Vite

During the development of the landing page using Vite, several dependencies were missing, resulting in errors. Specifically, dependencies such as framer-motion, @react-three/drei, and others were not installed.

Error Messages:


[plugin:vite:import-analysis] Failed to resolve import "framer-motion" from "src/App.jsx". Does the file exist?
Enter fullscreen mode Exit fullscreen mode

Solution:

To resolve these errors, we installed the missing packages using the following command:


npm install framer-motion @react-three/drei react-tilt react-vertical-timeline-component @emailjs/browser
Enter fullscreen mode Exit fullscreen mode

6. Addressing Git Push Errors

When attempting to push changes, the following error occurred:


! [rejected]        main -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/tobidelly/tobidellywebsite.git'
Enter fullscreen mode Exit fullscreen mode

Solution:

The push was rejected because the remote branch had changes that weren’t present locally. To resolve this, a force push was used after clearing the commit history:


git push --force origin main
Enter fullscreen mode Exit fullscreen mode

This command overwrites the remote branch with the local one, ensuring that the local changes are reflected on GitHub.

Top comments (0)