- Git is a powerful Version Control System (VCS) used by developers to track changes in their code.
- It allows multiple people to collaborate on the same project without overwriting each other's work.
- Think of Git as a time machine for your code—you can go back to any previous version at any time.
- GitHub is a web-based platform that hosts Git repositories (folders with version-controlled code).
- It acts as a central hub where developers can store their code, collaborate with others, and manage projects.
- While Git is the tool, GitHub is the service that enhances collaboration.
- Repository (Repo): A folder that contains your project's files and their version history.
- Local Repo: The repository on your computer where you make changes.
- Remote Repo: The repository stored on GitHub.
- Staged Files: Files prepared for the next commit (marked using
git add).
- Requirements: Install VS Code and Git Bash.
- Requirements: Install VS Code. The Terminal app is pre-installed; you can use it directly.
- Open your terminal (Git Bash on Windows, Terminal on Mac).
- Run the following command:
git --version- If Git is installed, it will display the installed version.
- If not, it will show an error indicating Git is not recognized.
Before using Git, configure your name and email (these details appear in your commits).
# Set your name
git config --global user.name "Your Name Here"
# Set your email
git config --global user.email "youremail@email.com"
# Check your configuration
git config --listThis configuration applies to all repositories on your computer.
-
Initialize a Repository: If your project is new, you need to initialize it as a Git repository.
git init
- This creates a hidden
.gitfolder in your project directory to track changes.
- This creates a hidden
-
Cloning an Existing Repository: If the project already exists on GitHub, clone it to your local machine.
git clone <repository_url>
- Replace
<repository_url>with the GitHub repository link.
- Replace
- If you initialized the repository locally, link it to a remote GitHub repository:
git remote add origin <repository_url>
originis the name of the remote.<repository_url>is the URL of your GitHub repository.
-
Checking Out the Remote Branch Directly on Local Device (Detached HEAD Mode):
git checkout origin/<branch-name>- Detached HEAD mode means you can view files, but any commits you make won't be attached to a branch.
- If you try to commit, Git will warn you that you're not on a branch.
-
Checking Out the Remote Branch on Local Device by creating branch:
git checkout -b <branch-name> origin/<branch-name>
- Now you are on a proper local branch that tracks origin/.
-
Stage Changes: Add all modified files to the staging area (preparing them for commit):
git add .- The
.stages all changes in the current directory.
- The
-
Commit Changes: Save the staged changes with a descriptive message:
git commit -m "Your commit message"- Example:
git commit -m "Added user authentication feature".
- Example:
Branches allow you to work on new features or fixes without affecting the main code.
-
Check Current Branch:
git branch
- The branch with an asterisk (
*) is your current branch.
- The branch with an asterisk (
-
Create a New Branch:
git branch <branch_name>
- Replace
<branch_name>with the desired name for your branch.
- Replace
-
Switch to a Branch:
git checkout <branch_name>
-
Create and Switch to a Branch:
git checkout -b <branch_name>
-
Rename the Current Branch:
git branch -M <new_branch_name>
-
Delete a Branch:
git branch -d <branch_name>
-
Push Changes to GitHub:
git push origin <branch_name>
- Upload your changes to the remote repository on the specified branch.
-
Pull Changes from GitHub:
git pull origin <branch_name>
- Fetch and merge updates from the specified branch of the remote repository.
If you need to pull updates but have uncommitted changes, stash them temporarily:
-
Save Changes to Stash:
git stash
- This clears the working directory without losing your changes.
-
Apply Stashed Changes:
git stash pop
-
Clear All Stashed Changes:
git stash clear
-
Check Current Status:
git status
- Look for files marked as modified (M), untracked (U), or staged for commit.
-
View Commit History:
git log
- Shows a history of commits.
- The output of git log looks like:
commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t Author: Your Name <youremail@example.com> Date: Wed Dec 27 10:30:00 2023 +0000 Added feature X to the projectHere, a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t is the commit hash representing the specific commit with commit message "Added feature X to the project"
-
Undo Staged Changes:
git reset <file_name>
- Removes the specified file from the staging area.
-
Undo the Last Commit:
git reset HEAD~1
- Keeps the changes but removes the commit.
-
Undo a Push:
- Option 1: Revert the commit (recommended for public branches):
git revert <commit_hash>
- Option 2: Reset the branch and force push:
git reset --hard <commit_hash> git push origin <branch_name> --force
- Option 1: Revert the commit (recommended for public branches):
If you initialized Git by mistake, remove the .git folder:
rm -r -force .git- This removes the repository but keeps your files.
This command will:
- Remove (rm) the hidden .git directory recursively (-r)
- Use -force flag since .git contains read-only files