Welcome! This guide is for beginners who want to set up and start using Git and GitHub efficiently.
- Getting Started with Git and GitHub/new_feature(branch)
- Table of Contents
- 1. Setting Up Your Git Credentials
- 2. Generating and Adding an SSH Key to GitHub
- 3. Creating a New Repository on GitHub
- 4. Cloning an Existing Repository
- 5. Basic Git Workflow
- 6. Common Git Commands
- 7. Resolving Merge Conflicts
- 8. Why Use Git and GitHub?
- 9. Visualizing Git Workflows
- 10. Pull Requests for Collaboration
- 11. Troubleshooting Common Issues
- 12. Git Cheat Sheet
Before you start using Git, configure your credentials to identify yourself in commits.
-
Set your GitHub username:
git config --global user.name "YourGitHubUsername"
-
Set your GitHub email (must match the email used for your GitHub account):
git config --global user.email "[email protected]"
-
Ensure your default branch name is
main
(important for new repositories):git config --global init.defaultBranch main
-
Verify your configuration:
git config --list
Sample output:
user.name=YourGitHubUsername [email protected] init.defaultBranch=main
Using SSH for authentication makes your workflow more secure and convenient.
-
Generate an SSH key:
ssh-keygen -t ed25519 -C "[email protected]"
- When prompted, press
Enter
to accept the default location. - Leave the passphrase empty for easier access.
- When prompted, press
-
Display your SSH public key:
cat ~/.ssh/id_ed25519.pub
-
Add the SSH key to your GitHub account:
- Go to GitHub SSH settings.
- Click "New SSH key".
- Paste your key and give it a meaningful title.
- Click "Add SSH key".
-
Test your SSH connection:
ssh -T [email protected]
If successful, you'll see a message like:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
-
Create a repository on GitHub:
- Go to GitHub and click "New Repository".
- Choose a name, description, and visibility (public/private).
- Click "Create repository".
-
Initialize a Git repository locally:
git init
-
Add a remote repository:
git remote add origin [email protected]:yourusername/your-repo-name.git
-
Add and commit files:
git add . git commit -m "Initial commit"
-
Push changes to GitHub:
git push -u origin main
If you want to work on an existing project, clone it to your local machine:
git clone [email protected]:username/repository-name.git
Navigate to the project directory:
cd repository-name
-
Check the status of your repository:
git status
-
Stage your changes:
git add .
-
Commit the changes with a message:
git commit -m "Updated feature"
-
Push your changes to GitHub:
git push
Here are some common Git commands you might find useful:
-
Check the current branch:
git branch
-
Create a new branch:
git checkout -b new-branch-name
-
Switch to an existing branch:
git checkout branch-name
-
Merge a branch into the current branch:
git merge branch-name
-
Delete a branch:
git branch -d branch-name
Merge conflicts occur when changes from different branches conflict with each other. Here's how to resolve them:
-
Identify the files with conflicts:
git status
-
Open the conflicted files and manually resolve the conflicts. Look for conflict markers (
<<<<<<<
,=======
,>>>>>>>
) and edit the file to resolve the conflicts. -
After resolving the conflicts, stage the resolved files:
git add resolved-file
-
Commit the resolved changes:
git commit -m "Resolved merge conflicts"
Git and GitHub are essential tools for managing and collaborating on projects. Here are some common use cases:
- Collaborative Coding: Work together on the same codebase while tracking who made which changes.
- Open-Source Contributions: Contribute to projects like React, Linux, or TensorFlow.
- Backup and Version Control: Safeguard your code and revisit previous versions when needed.
- Portfolio Building: Showcase your projects to potential employers by hosting them on GitHub.
A simple branching workflow:
main
|
|-------- feature-branch
| |
| |------- Bug fix
| | |
| | |----- Merge into main
Branches allow you to isolate features, bug fixes, or experiments from the main codebase, avoiding conflicts.
Pull requests (PRs) are how developers suggest changes in a shared repository. They are great for:
- Discussing changes with collaborators.
- Reviewing code to ensure quality and consistency.
- Merging changes into the main branch after approval.
-
Push your branch to GitHub:
git push origin your-branch-name
-
Go to your repository on GitHub.
-
Click Pull Requests > New Pull Request.
-
Select the branch to merge and submit your request!
1. SSH Key Issues
- Problem:
Permission denied (publickey)
when trying to push. - Solution:
- Ensure your SSH key is added to GitHub (steps here).
- Check if the SSH agent is running:
eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_ed25519
2. Conflicts During Merge
- Problem: Git shows a merge conflict after pulling changes.
- Solution:
- Use
git status
to identify conflicted files. - Open the conflicted files, manually resolve the conflict, then:
git add resolved-file git commit -m "Resolved conflicts"
- Use
# Basic Git Commands
git init # Initialize a new repository
git clone <url> # Clone a repository
git branch # List branches
git checkout -b <name> # Create and switch to a new branch
git add . # Stage changes
git commit -m "<msg>" # Commit changes
git push origin <branch> # Push branch to remote
git pull # Pull latest changes
Congratulations! You've successfully set up your GitHub credentials, created an SSH key, and pushed your first project to GitHub. Happy coding! 🚀