Git is a version control system that helps developers manage and track changes in their code, while GitHub is a cloud-based platform for hosting Git repositories and collaborating with others. This guide covers the essential Git commands and workflows to get you started.
Git is a distributed version control system that allows multiple developers to work on a project efficiently, keeping track of changes and enabling easy collaboration.
| Command | Description |
|---|---|
git init |
Initializes a new Git repository in your local project directory. |
git clone <repo_url> |
Downloads (clones) a repository from GitHub to your local machine. |
git status |
Shows the current state of your repository (modified, staged, or untracked files). |
git add <file> |
Adds a specific file to the staging area before committing. |
git add . |
Stages all modified and new files in the repository. |
git commit -m "message" |
Saves (commits) the staged changes with a meaningful message. |
git push origin <branch> |
Uploads (pushes) local commits to a remote GitHub repository on the specified branch. |
git pull origin <branch> |
Fetches and merges the latest changes from a remote repository into your local branch. |
git log |
Displays a history of commits made in the repository. |
git branch |
Lists all branches in the repository and highlights the current branch. |
git checkout -b <new-branch> |
Creates and switches to a new branch for feature development. |
git merge <branch> |
Merges changes from another branch into the current branch. |
git reset --hard HEAD~1 |
Removes the last commit permanently from history. |
git revert <commit_id> |
Creates a new commit that undoes a previous commit without modifying history. |
git checkout -- <file> |
Discards changes made to a specific file before committing. |
git remote -v |
Displays the URLs of remote repositories linked to your project. |
git stash |
Temporarily saves changes that are not yet committed, allowing you to switch branches. |
git stash pop |
Restores the most recently stashed changes. |
git rm <file> |
Removes a file from the repository and deletes it from the working directory. |
GitHub is a web-based platform that hosts Git repositories, making it easy for developers to collaborate on projects, track issues, and review code.
-
Create a Repository
- Go to GitHub β Click New Repository β Enter a name β Click Create Repository
-
Clone the Repository (Download it Locally)
git clone <repo_url> cd <repo_name>
Follow these steps to create and push a new GitHub repo using terminal:
mkdir <project_name>
cd <project_name>
git initecho "# Project Title" > README.md
git add .
git commit -m "Initial commit"- Go to GitHub β New Repository β Enter name β Click Create Repository
- DO NOT initialize with README, .gitignore, or license.
git remote add origin https://github.com/<your_username>/<repo_name>.git
git branch -M main
git push -u origin mainFollow these steps to add and upload a new file to your GitHub repository:
- Open VS Code and navigate to your project folder.
- Add a new file or modify an existing file in the project directory.
- Open the terminal (
Ctrl + ~on Windows/Linux orCmd + ~on Mac).
git status- This shows any new, modified, or deleted files.
git add <filename> # Adds a specific file
git add . # Adds all new and modified filesgit commit -m "Added <filename>"- Write a meaningful commit message describing the change.
git push origin main- If you're working on a different branch, replace
mainwith your branch name.
- Go to your GitHub repository and check if the file is uploaded.
π Now your file is successfully added to GitHub! π
Follow these steps to remove a file from your GitHub repository:
- Open VS Code and navigate to your project folder.
- Open the terminal (
Ctrl + ~on Windows/Linux orCmd + ~on Mac).
git rm <filename>- This removes the file from your local repository.
git status- Verify that the file is marked as deleted.
git commit -m "Removed <filename>"- Write a meaningful commit message describing the removal.
git push origin main- If you're working on a different branch, replace
mainwith your branch name.
- Go to your GitHub repository and check if the file is removed.
π Now your file is successfully removed from GitHub! ποΈ