Here is a detailed list of useful Git commands, along with illustrative examples:
Initialize a new Git repository in your project directory.
$ 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.
$ 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.
$ 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.
$ git commit -m "Fix a bug in login process" [main 83a9b47] Fix a bug in login process 1 file changed, 5 insertions(+), 2 deletions(-)
View the current status of the repository, including modified files and the staging area.
$ 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")
Display the commit history of the repository.
$ 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
List all branches in the repository and mark the current branch.
$ git branch * main feature/add-new-feature feature/fix-bug
git checkout [branch]
Switch to another branch in the repository.
$ git checkout feature/fix-bug Switched to branch 'feature/fix-bug'
git merge [branch]
Merge another branch into the current branch.
$ 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
Fetch and integrate changes from a remote repository into the current branch.
$ git pull origin main From https://github.com/yourusername/your-repo * branch main -> FETCH_HEAD Already up to date.
Push changes from the current branch to a remote repository.
$ git push origin main
git remote add [name] [url]
Add a new remote server to your list of remote repositories.
$ git remote add upstream https://github.com/upstream-repo/repo.git
Download changes from remote repositories but don't integrate into the current branch.
$ git fetch origin
Compare changes between the staging area and the tracked files.
$ git diff
git reset [file]
Remove a file from the staging area and revert it to the previous state.
$ git reset index.html
emporarily save uncommitted changes to work on a different branch without committing them.
$ 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.
$ 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)