Git is an indispensable tool for modern software development, enabling efficient version control and collaboration. This guide will walk you through the fundamental Git commands every beginner should know, from installation to basic workflow.
Getting Started with Git
Installation and Setup
Before diving into Git commands, you need to install Git on your system:
-
Download Git from the official website: git-scm.com
-
After installation, verify the installed version:
Terminalgit --version
or
Terminalgit -v
Initial Configuration
Configure your Git identity with these global settings:
git config --global user.name "Your Name"
git config --global user.email "You@Email.com"
git config --global init.defaultBranch main
These commands set up your name, email, and default branch name for all your Git repositories.
Essential Git Commands for Beginners
Creating and Cloning Repositories
-
Initialize a new repository:
Terminalgit init
Use this command in an existing project directory to start tracking it with Git.
-
Clone an existing repository:
Terminalgit clone <url-from-github>
Example:
Terminalgit clone https://github.com/Edward-126/template-next.git
This command creates a local copy of a remote repository.
Checking Repository Status
To see the current state of your repository:
git status
For a more concise status overview:
git status -s
Adding and Committing Changes
-
Add files to the staging area:
Terminalgit add <filename>
Example:
Terminalgit add index.html
To add all changed files:
Terminalgit add .
-
Commit staged changes:
Terminalgit commit -m "Short message"
This creates a new commit with the staged changes and a descriptive message.
-
Combine adding and committing:
Terminalgit commit -a -m "Short message"
This command stages all modified files and creates a new commit in one step.
Managing Changes
-
Discard changes in a file:
Terminalgit restore <filename>
-
Unstage a newly added file:
Terminalgit rm --cached <filename>
-
Unstage a previously modified file:
Terminalgit restore --staged <filename>
Connecting to GitHub
To link your local repository to GitHub:
git remote add origin <gh-url>
git branch -M main
git push -u origin main
Replace
with your GitHub repository URL.<gh-url>
Understanding File Statuses
Git tracks the status of your files:
- Untracked: New files not yet added to the repository (U in VS Code)
- Unmodified: Tracked files without changes
- Modified: Tracked files with changes (M in VS Code)
- Staged: Files added to the staging area, ready for commit (A in VS Code)
- Ignored: Files matching patterns in
.gitignore
(grayed out in VS Code)
Basic Git Workflow
- Create new files or make changes to existing files.
- Stage and commit changes:
Terminal
git commit -a -m "Short detail message about changes"
- Push changes to the remote repository:
Terminal
git push
Pro Tip: Avoiding Repeated Credential Requests
To prevent Git from asking for credentials with every push:
git config --global credential.helper 'cache --timeout=3600'
This caches your credentials for an hour (3600 seconds).
Conclusion
This guide covers the essential Git commands for beginners. As you become more comfortable with these basics, you can explore more advanced Git features to enhance your development workflow. Remember, practice is key to mastering Git, so don't hesitate to experiment in your projects!
Happy coding, and feel free to reach out if you have any questions or need further clarification on any Git concepts!