Initialize a New Git Repository:
To start tracking changes in a project, navigate to the project's root directory and run:
git init
Clone a Repository: To make a copy of an existing Git repository from a remote location (e.g., GitHub), use:
git clone <repository_url>
Configure Git: Set your name and email address, which are used to identify your commits:
git config --global user.name "Your Name" git config --global user.email "youremail@example.com"
Check the Status of Your Repository: To see the current status of your repository and understand which files have been modified and staged, use:
git status
Output:
On branch main Your branch is up to date with 'origin/main'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in the working directory) modified: file1.txt modified: file2.txt Untracked files: (use "git add <file>..." to include in what will be committed) newfile.txt no changes added to commit (use "git add" and/or "git commit -a")
Add Changes to the Staging Area: Stage changes for commit using:
git add <file1> <file2> ...
To stage all changes, use:
git add .
Commit Changes: The git commit command in Git is used to create a new commit in your Git repository. It records changes, creates a unique identifier, saves a commit message, and more. Use it as follows:
git commit -m "Your commit message here"
The
-m
stands for "message," and you follow it with a meaningful commit message enclosed in double quotes.View Commit History: View a list of commits in the repository:
git log
Create a Branch: To create a new branch for working on a feature or bug fix, use:
git branch <branch_name>
Switch Branches: Change to a different branch using:
git checkout <branch_name>
List All Branches: List all local branches:
git branch
To see remote branches:
git branch -r
To see all local and remote branches:
git branch -a
To create and move to a new branch:
git checkout -b my-branch-name
Merge Branches: Merge changes from one branch into another:
git checkout <target_branch> git merge <source_branch>
Pull Changes from a Remote Repository: To fetch the latest changes from a remote repository and merge them into the current branch, use:
git pull <remote> <branch>
Push Changes to a Remote Repository: Push your local changes to a remote repository:
git push origin <branch_name>
Create Tags: Create annotated (version) tags for specific commits:
git tag -a v1.0 -m "Version 1.0"
Git Restore: Use "git restore --staged ..." to discard changes in the working directory. Suggests using
git restore
to discard changes in specific files in the working directory.Git Stash: The git stash command is used to temporarily save and set aside uncommitted changes in your working directory. Here are common stash commands:
Stash Changes:
git stash
List Stashes:
git stash list
Apply a Stash:
git stash apply
Apply and Remove a Stash:
git stash pop
Apply a Specific Stash:
git stash apply stash@{1}
Drop a Stash:
git stash drop stash@{1}
Clear All Stashes:
git stash clear
Save Stash with a Description:
git stash save "Description of the stash"
Create a New Branch from a Stash:
git stash branch new-branch-name stash@{1}
Git Remote Add: The
git remote add
command is used to establish a connection between your local Git repository and a remote repository. Use it as follows:git remote add origin <remote_repository_url>
Git Push: The git push command is used to upload your local branch's commits and changes to a remote repository. Use it like this:
git push <remote> <branch>
For example:
git push origin main
- Extra: Git has many more features and commands, so refer to the official Git documentation or use
git --help
for more information on specific commands.