Here is a detailed list of useful Git commands, along with illustrative examples:
git init
Initialize a new Git repository in your project directory.
Example:
$ git init
Initialized empty Git repository in /path/to/your/project/.git/
git clone [url]
Clone a remote repository from the server to your local machine.
Example:
$ git clone https://github.com/yourusername/your-repo.git
Cloning into 'your-repo'...
git add [file]
Add one or more files to the staging area to prepare for a commit.
Example:
$ git add index.html
$ git add *.css
git commit -m "message"
Create a new commit with the changes that have been added to the staging area and include your commit message.
Example:
$ git commit -m "Fix a bug in login process"
[main 83a9b47] Fix a bug in login process
1 file changed, 5 insertions(+), 2 deletions(-)
git status
View the current status of the repository, including modified files and the staging area.
Example:
$ git status
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: index.html
no changes added to commit (use "git add" and/or "git commit -a")
git log
Display the commit history of the repository.
Example:
$ git log
commit 83a9b4713f9b6252bfc0367c8b1ed3a8e9c75428 (HEAD -> main)
Author: Your Name <[email protected]>
Date: Mon Jul 13 12:34:56 2023 +0200
Fix a bug in login process
commit 47f1c32798b7e862c4c69718abf6498255f1a3d2
Author: Your Name <[email protected]>
Date: Sun Jul 12 18:42:15 2023 +0200
Add new homepage
git branch
List all branches in the repository and mark the current branch.
Example:
$ git branch
* main
feature/add-new-feature
feature/fix-bug
git checkout [branch]
Switch to another branch in the repository.
Example:
$ git checkout feature/fix-bug
Switched to branch 'feature/fix-bug'
git merge [branch]
Merge another branch into the current branch.
Example:
$ git merge feature/add-new-feature
Updating 83a9b47..65c6017
Fast-forward
new-feature.html | 10 ++++++++++
1 file changed, 10 insertions(+)
create mode 100644 new-feature.html
git pull
Fetch and integrate changes from a remote repository into the current branch.
Example:
$ git pull origin main
From https://github.com/yourusername/your-repo
* branch main -> FETCH_HEAD
Already up to date.
git push
Push changes from the current branch to a remote repository.
Example:
$ git push origin main
git remote add [name] [url]
Add a new remote server to your list of remote repositories.
Example:
$ git remote add upstream https://github.com/upstream-repo/repo.git
git fetch
Download changes from remote repositories but don't integrate into the current branch.
Example:
$ git fetch origin
git diff
Compare changes between the staging area and the tracked files.
Example:
$ git diff
git reset [file]
Remove a file from the staging area and revert it to the previous state.
Example:
$ git reset index.html
git stash
emporarily save uncommitted changes to work on a different branch without committing them.
Example:
$ git stash
Saved working directory and index state WIP on feature/branch: abcd123 Commit message
git remote -v
List the remote servers and their url addresses.
Example:
$ git remote -v
origin https://github.com/yourusername/your-repo.git (fetch)
origin https://github.com/yourusername/your-repo.git (push)
upstream https://github.com/upstream-repo/repo.git (fetch)
upstream https://github.com/upstream-repo/repo.git (push)