Master the basics of version control with Git and collaborate effectively using GitHub. Your journey to efficient coding starts here.
By Upingi Team / Tutorial Level: Beginner
Why Learn Git & GitHub?
Git is the industry standard for version control, allowing you to track changes in your code, revert to previous states, and manage different features simultaneously. GitHub is a platform that hosts your Git repositories, enabling collaboration, code sharing, and project management.
Mastering these tools is crucial for any developer, designer working with code, or anyone collaborating on digital projects.
Prerequisites
- Install Git: Download and install Git for your operating system from the official Git website.
- Create a GitHub Account: Sign up for a free account at GitHub.com.
- Basic Command Line Familiarity: You should be comfortable opening a terminal or command prompt and navigating directories.
We'll guide you through the rest!
Chapter 1: Your First Local Repository
Let's start by creating a project folder and initializing Git.
- Create a project folder: Make a new directory for your project (e.g., `my-git-project`) and navigate into it using your terminal.
- Initialize Git: Run the command `git init`. This creates a hidden `.git` directory where Git stores its tracking information.
- Check Status: Use `git status` to see the current state of your repository. Initially, it will show no commits yet.
- Create a file: Add a simple file, like `README.md`.
- Stage the file: Run `git add README.md` (or `git add .` to stage all changes). Staging tells Git which changes you want to include in the next snapshot (commit).
- Commit changes: Run `git commit -m "Initial commit: Add README"`. This saves the staged changes as a snapshot with a descriptive message.
You've made your first commit! Use `git log` to see the history.
Chapter 2: Pushing to GitHub
Now, let's connect your local repository to a remote one on GitHub.
- Create a GitHub Repository: Go to GitHub.com, click the '+' icon, and select "New repository". Give it a name (e.g., `my-git-project`), keep it public, and *do not* initialize it with a README, .gitignore, or license (since you already have a local repo).
- Get the Remote URL: Copy the HTTPS or SSH URL provided by GitHub for your new repository.
- Add Remote: In your terminal, run `git remote add origin `. This links your local repo to the remote one, naming it `origin` by convention.
- Verify Remote: Use `git remote -v` to check that the remote was added correctly.
- Push Changes: Run `git push -u origin main` (or `master` if that's your default branch name). The `-u` flag sets the upstream branch, so future pushes can simply use `git push`.
Refresh your GitHub repository page – your `README.md` should now be visible!
Chapter 3: Basic Branching (Placeholder)
Placeholder explaining the concept of branches (`git branch feature-x`), switching branches (`git checkout feature-x`), making changes, committing on the branch, switching back (`git checkout main`), and merging (`git merge feature-x`). Mention potential merge conflicts briefly.
Conclusion & Next Steps
Congratulations! You've learned the fundamental workflow of Git and GitHub: initializing a repository, staging and committing changes, connecting to a remote repository, pushing your work, and the basics of branching.
Where to go from here?
- Explore `git pull` to fetch changes from the remote.
- Learn more about resolving merge conflicts.
- Investigate `.gitignore` files to exclude unnecessary files.
- Practice the branching workflow for feature development.
- Look into pull requests on GitHub for collaboration.