Common Git Commands
Git is a powerful version control system that allows developers to manage their codebase effectively. Here are some of the most common Git commands that you should be familiar with:
1. git init
This command is used to initialize a new Git repository in the current directory. It creates a hidden .git
folder, which is where Git stores all the version control information.
Example:
$ git init
Initialized empty Git repository in /path/to/your/project/.git/
2. git clone
This command is used to create a copy of an existing Git repository. It downloads the entire repository, including all the files, branches, and commit history.
Example:
$ git clone https://github.com/username/repository.git
Cloning into 'repository'...
remote: Counting objects: 100, done.
remote: Compressing objects: 100% (80/80), done.
remote: Total 100 (delta 20), reused 100 (delta 20)
Unpacking objects: 100% (100/100), done.
3. git add
This command is used to stage changes in the working directory for the next commit. It adds the specified files or directories to the staging area.
Example:
$ git add file1.txt file2.txt
$ git add . # Adds all changed files in the current directory
4. git commit
This command is used to create a new commit with the changes that have been staged. It's important to include a meaningful commit message to describe the changes made.
Example:
$ git commit -m "Implement new feature X"
[master 1234567] Implement new feature X
2 files changed, 50 insertions(+)
5. git push
This command is used to upload your local repository changes to a remote repository, such as GitHub or GitLab.
Example:
$ git push origin master
Counting objects: 10, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (10/10), 1.24 KiB | 1.24 MiB/s, done.
Total 10 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), done.
To https://github.com/username/repository.git
1234567..89abcde master -> master
6. git pull
This command is used to download changes from a remote repository and merge them into your local repository.
Example:
$ git pull origin master
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 5 (delta 2), reused 5 (delta 2)
Unpacking objects: 100% (5/5), done.
From https://github.com/username/repository
* branch master -> FETCH_HEAD
Updating 1234567..89abcde
Fast-forward
file1.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
7. git status
This command is used to display the current state of the working directory and the staging area. It shows which files have been modified, added, or deleted.
Example:
$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: file1.txt
new file: file2.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
file3.txt
8. git log
This command is used to display the commit history of the repository. It shows the commit messages, authors, dates, and the commit hashes.
Example:
$ git log
commit 89abcde (HEAD -> master, origin/master)
Author: John Doe <[email protected]>
Date: Fri Apr 14 15:23:45 2023 -0400
Implement new feature X
commit 1234567
Author: Jane Smith <[email protected]>
Date: Wed Apr 12 10:15:32 2023 -0400
Fix bug in file1.txt
These are just a few of the most common Git commands. Git has a wide range of features and commands that can help you manage your codebase effectively. It's important to understand the basic commands and their usage to become proficient in using Git.
Here's a Mermaid diagram that summarizes the key Git commands:
This diagram shows the typical workflow of using Git, starting from initializing a new repository, cloning an existing one, adding changes, committing them, pushing to a remote repository, and then pulling the latest changes from the remote repository. The git status
and git log
commands can be used at any point to check the current state of the repository.
By mastering these common Git commands, you'll be able to effectively manage your codebase, collaborate with other developers, and keep track of the changes made over time.