A Comprehensive Guide to Setting Up and Using Git from the Terminal
Git is a powerful version control system used by developers worldwide to manage and collaborate on code. When working with Git, the terminal becomes your best friend for initializing repositories, pushing code to GitHub, and more. This guide walks you through everything from setting your identity in Git to pushing your code for the first time.
Set Your Git Identity
Before using Git, you need to tell it who you are. Git uses this information to associate your commits with your identity.
Global Identity (Applies to All Repositories)
To set your name and email for all repositories, run:
git config --global user.name "Your Full Name"
git config --global user.email "your-email@example.com"
Verify Your Identity
To check your configured identity:
git config --global --list
You should see something like:
user.name=John Doe
user.email=johndoe@example.com
Initialize a Git Repository
To start using Git in your project folder, initialize a repository:
git init
This creates a .git
folder that tracks changes in your project.
Add Files to Git
To track your project files with Git, add them to the staging area:
git add .
This stages all the files in your project for the next commit. You can also add specific files:
git add <file-name>
A Comprehensive Guide to Setting Up and Using Git from the Terminal
Git is a powerful version control system used by developers worldwide to manage and collaborate on code. When working with Git, the terminal becomes your best friend for initializing repositories, pushing code to GitHub, and more. This guide walks you through everything from setting your identity in Git to pushing your code for the first time.
Set Your Git Identity
Before using Git, you need to tell it who you are. Git uses this information to associate your commits with your identity.
Global Identity (Applies to All Repositories)
To set your name and email for all repositories, run:
bashКопировать кодgit config --global user.name "Your Full Name"
git config --global user.email "your-email@example.com"
Repository-Specific Identity
If you only want to set your identity for the current repository:
bashКопировать кодgit config user.name "Your Full Name"
git config user.email "your-email@example.com"
Verify Your Identity
To check your configured identity:
bashКопировать кодgit config --global --list
You should see something like:
cssКопировать кодuser.name=John Doe
user.email=johndoe@example.com
Initialize a Git Repository
To start using Git in your project folder, initialize a repository:
bashКопировать кодgit init
This creates a .git
folder that tracks changes in your project.
Add Files to Git
To track your project files with Git, add them to the staging area:
bashКопировать кодgit add .
This stages all the files in your project for the next commit. You can also add specific files:
bashКопировать кодgit add <file-name>
Commit Your Changes
A commit is like a snapshot of your project at a specific point. To create a commit:
git commit -m "Your commit message"
The message should describe the changes you’ve made, e.g., Added new homepage design
.
Connect to a GitHub Repository
If your project isn’t already connected to a GitHub repository, you need to set it up. First, create a repository on GitHub. Then, link it to your local project:
git remote add origin https://github.com/username/repository-name.git
Push Code to GitHub for the First Time
To upload your local code to GitHub, push it to the remote repository:
git push -u origin main
If your default branch is master
(instead of main
):
git push -u origin master
What If You See remote origin already exists
?
This error occurs if the remote repository has already been linked. To fix it:
- Update the Remote URL:bashКопировать код
git remote set-url origin https://github.com/username/repository-name.git
Alternatively, Remove and Re-add the Remote:
git remote remove origin
git remote add origin https://github.com/username/repository-name.git
Check Repository Status
To see the current state of your repository:
git status
This fetches the latest changes from the main
branch and merges them into your local branch.
Push Updates to GitHub
When you make new changes and commit them, push the updates to GitHub:
git push origin main
my github: https://github.com/khaledyou87