The most commonly used Git commands.
Git is a powerful version control system that allows developers to manage their source code and collaborate with others on software projects. Here are some of the most commonly used Git commands, along with examples of how to use them:
Creating a Repository
git init
- Initializes an empty Git repository in the current directory.
Example:
dir my_project
$ cd my_project
$ git init
git clone
- Clones an existing Git repository from a remote source.
Example:
$ git clone https://github.com/myusername/myrepo.git
Making Changes
git add
- Adds changes to the staging area.
Example:
$ git add file1.txt file2.txt
git commit
- Commits changes to the repository.
Example:
$ git commit -m "Added new feature"
git status
- Shows the status of the repository, including any uncommitted changes.
Example:
$ git status
Branches
git branch
- Lists all the branches in the repository.
Example:
$ git branch
feature
* master
git checkout
- Switches to a different branch.
Example:
$ git checkout feature
Switched to branch 'feature'
git merge
- Merges changes from one branch into another.
Example:
$ git merge feature
git remote
- Shows the remote repositories that are currently configured.
Example:
$ git remote
origin
git push
- Pushes local changes to a remote repository.
Example:
$ git push origin master
git pull
- Pulls changes from a remote repository and merges them with the local repository.
Example:
$ git pull origin master
Viewing History
git log
- Shows the commit history of the repository.
Example:
$ git log
commit 670b8f7aa07a34237f5c1c02e5ed5e5f37d5ce2b
Author: Ajay Dhangar <ajaydhangar@example.com>
Date: Wed Feb 16 15:34:06 2023 -0500
Added new feature
commit b2ee1148f2d2e66e0d3e84056b1da3b1a4279f6d
Author: Ajay Dhangar <ajaydhangar@example.com>
Date: Wed Feb 16 15:20:23 2023 -0500
Fixed bug in login screen
git diff
- Shows the differences between the current state of the repository and a previous commit.
Example:
$ git diff HEAD~1
These are just a few of the most commonly used Git commands. For a more complete reference, you can refer to the official Git documentation.