This guide explains the main Git commands.
- Setting Up Git
- Creating a Repository
- Checking the Status
- Adding Files
- Deleting Files
- Committing Changes
- Creating a New Branch
- Link to a Remote Repository (GitHub)
- Pushing Changes
- Example of First Initialization of (remote) Repository
Before you start using Git, you need to configure your username and email:
git config --global user.name "Your Name"
git config --global user.email "your.emailexample.com"
This sets your identity for all Git repositories on your system.
You can omit the --global
flag if you want to configure only the current repository.
To create a new Git repository:
git init
This initializes a new Git repository in your current directory.
To check the current status of your repository:
git status
This shows the status of your working directory and staging area.
# To add a new file to your repository
git add <file-name>
# To add multiple files to your repository
git add <file-name-a> <file-name-b>
To add all files in the current directory:
git add .
REMEMBER to commit the changes!
To delete a file from your working directory and index:
git rm <file-name>
REMEMBER to commit the changes!
To save your changes to the repository:
git commit -m "Meaningful message that describes the changes you made"
To create and switch to a new branch:
git checkout -b <branch-name>
This creates a new branch and switches to it.
Create a repository on GitHub and link it to your local repository:
git remote add origin https://github.com/<Your-repository-name>.git
To push a branch to the remote repository:
git push -u origin <branch-name>
This command explicitly specifies that you want to push a particular branch (<branch-name>
) to the remote repository named origin
. Default name of remote repository on GitHub is origin
.
The -u
flag in git push -u origin main
stands for --set-upstream
and is used to set the remote branch as the upstream branch for the local branch. This means the local branch will be linked to the specified remote branch, allowing you to use shortened commands in the future. For example:
git push
git pull
# Create and initialize the .git folder
git init
# Add all files of the current folder to the stage
git add .
# Describe your commit with a message (example: "first commit")
git commit -m "first commit"
# Create main/master branch with name "main"
git branch -M main
# Link your local repository to your remote GitHub repository, already created on GitHub (example: "https://github.com/yursds/git_initRepo.git")
# Default name of remote repository on GitHub is origin
git remote add origin https://github.com/yursds/git_initRepo.git
# Push "origin" to "main"
git push -u origin main