Welcome to the comprehensive Git and Pull Request tutorial for the Xplore project! This guide will walk you through the entire process of contributing to the project, from forking the repository to creating a pull request. We'll cover basic and advanced Git commands, error handling, and best practices.
- Getting Started
- Basic Git Commands
- Making Changes
- Advanced Git Commands
- Creating a Pull Request
- Troubleshooting
- Best Practices
- Interactive Practice
- Visit the Xplore repository on GitHub.
- Click the "Fork" button in the top-right corner to create a copy in your account.
Clone your forked repository to your local machine:
git clone https://github.com/YOUR_USERNAME/Xplore.git
Replace YOUR_USERNAME
with your GitHub username.
cd Xplore
Add the original repository as a remote to keep your fork updated:
git remote add upstream https://github.com/SharanRP/Xplore.git
View the status of your working directory:
git status
Create and switch to a new branch:
git checkout -b feature/your-feature-name
List all branches:
git branch
Switch to an existing branch:
git checkout branch-name
- Make your desired changes to the codebase.
- Stage your changes:
git add .
- Commit your changes:
git commit -m "Add feature xyz"
- Push your changes to your fork:
git push origin feature/your-feature-name
Temporarily store changes:
git stash
Apply stashed changes:
git stash pop
Fetch updates from the original repository:
git fetch upstream
Merge updates from the original repository:
git checkout main
git merge upstream/main
Rebase your feature branch:
git checkout feature/your-feature-name
git rebase main
git reset
is a powerful command for undoing changes and managing your commit history. Use it carefully to avoid losing work.
- Soft reset (
--soft
): Moves HEAD but doesn't change staging area or working directory. - Mixed reset (
--mixed
or default): Moves HEAD and updates staging area. - Hard reset (
--hard
): Moves HEAD, updates staging area and working directory.
- Undo last commit (keep changes):
git reset HEAD~1
- Undo multiple commits:
git reset HEAD~n
(replacen
with number of commits) - Reset to specific commit:
git reset commit-hash
- Unstage changes:
git reset HEAD file-name
- Discard all local changes (use cautiously):
git reset --hard HEAD
- Go to your forked repository on GitHub.
- Switch to your new branch.
- Click "New Pull Request".
- Select the base repository and branch you want to merge into.
- Provide a title and description for your changes.
- Click "Create Pull Request".
-
"fatal: not a git repository"
- Solution: Ensure you're in the correct directory or initialize with
git init
.
- Solution: Ensure you're in the correct directory or initialize with
-
"fatal: remote origin already exists"
- Solution: Update the remote URL:
git remote set-url origin https://github.com/YOUR_USERNAME/Xplore.git
- Solution: Update the remote URL:
-
"error: failed to push some refs"
- Solution: Pull the latest changes before pushing:
git pull origin main
- Solution: Pull the latest changes before pushing:
-
"You are in 'detached HEAD' state"
- Solution: Return to the original branch:
git checkout branch-name
- Solution: Return to the original branch:
- Keep commits small and focused on a single change.
- Write clear, concise commit messages.
- Pull the latest changes from upstream before starting new work.
- Use branches for new features or bug fixes.
- Review your changes before committing.
- Test your changes thoroughly before creating a pull request.
Try this exercise to practice the Git workflow:
- Create a new branch called
practice/your-name
. - Create a new file called
your-name.md
with some content about yourself. - Stage and commit this file.
- Push the branch to your fork.
- Create a pull request for this branch.
- Make a change to your file, commit, and push again.
- Use
git reset
to undo the last commit while keeping the changes. - Recommit the changes with a different commit message.
- Force push the branch to update your pull request.
Please add your name and registration number below:
- Name: [Your Name]
- Registration Number: [Your Registration Number]
Congratulations on completing this comprehensive Git and Pull Request tutorial! Remember, practice is key to mastering Git. Don't hesitate to experiment in a test repository to gain more confidence with these commands.
If you have any questions or encounter any issues, please don't hesitate to ask for help. Happy coding! "# INH-git"