How to Use Git and GitHub

 

1. The Core Concept: The Git Workflow

To understand Git, you must understand the three stages a file goes through before it is saved permanently:

  1. Working Directory: Where you are currently making changes to your files.

  2. Staging Area (Index): A "prep zone" where you pick which changes are ready to be saved.

  3. Local Repository: Where Git permanently stores the versions (commits) of your project.

  4. Remote Repository (GitHub): The online version of your project where your local code is uploaded.


2. Essential Git Commands

Once you have Git installed, you will use these commands in your terminal or command prompt:

Setup & Starting

  • git init: Creates a new Git repository in your current folder.

  • git clone [URL]: Downloads an existing project from GitHub to your computer.

The Daily Cycle

  • git status: Shows which files have been modified and what is in the staging area.

  • git add [filename]: Moves a file to the Staging Area. Use git add . to add everything.

  • git commit -m "message": Saves your changes to the Local Repository with a descriptive note.

  • git push: Sends your local commits to GitHub.

  • git pull: Downloads the latest changes from GitHub to your computer.


3. Branching: The "Parallel Universe."

Branching is Git's most powerful feature. It allows you to work on a new feature without breaking the "Main" (working) version of your code.

  • git branch [name]: Creates a new branch.

  • git checkout [name]: Switches you to that branch.

  • git merge [name]: Combines the changes from your feature branch back into the main branch.


4. How to Use GitHub

While Git is the engine, GitHub is the platform. Here is how to get a project live:

  1. Create a Repo: On GitHub, click "New Repository."

  2. Link Local to Remote: Run the command GitHub provides (usually git remote add origin [URL]).

  3. Push Code: Use git push -u origin main to send your code to the site.

  4. Pull Requests (PRs): If you are working on a team, you submit a Pull Request to ask the owner to "pull" your changes into their project.


5. Summary Table: Git vs. GitHub

FeatureGitGitHub
What is it?Software (Tool)Service (Website)
InstallationInstalled locally on your PC.Accessed via a web browser.
PurposeTracks version history.Hosts and shares Git repositories.
InternetWorks offline.Requires internet to upload/download.
InterfaceCommand Line (usually).Graphical User Interface (GUI).

Common "Save Me" Command

If you ever get stuck or want to see your history, use:git log --oneline --graph --all

This provides a visual map of your commits and branches directly in your terminal.

Would you like me to guide you through your first "Commit and Push" to GitHub, or would you prefer to learn about resolving Merge Conflicts?

Post a Comment

0 Comments