What are the common Git commands?

Common Git Commands

Git is a distributed version control system that is widely used in software development. It allows developers to track changes in their codebase, collaborate with others, and manage project history effectively. Here are some of the most common Git commands that developers use on a regular basis:

1. git init

The git init command is used to initialize a new Git repository in the current directory. This creates a hidden .git directory that stores all the necessary files and configurations for the repository.

Example:

$ git init
Initialized empty Git repository in /path/to/your/project/.git/

2. git clone

The git clone command is used to create a local copy of an existing remote repository. This is useful when you want to start working on a project that is hosted on a remote server, such as GitHub or GitLab.

Example:

$ git clone https://github.com/user/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

The git add command is used to stage changes in the working directory for the next commit. This command adds the specified files or directories to the staging area, which is a temporary holding area for changes.

Example:

$ git add file1.txt file2.txt
$ git add .  # Add all changes in the current directory and its subdirectories

4. git commit

The git commit command is used to create a new commit with the changes that have been staged using the git add command. Each commit represents a snapshot of the project at a specific point in time.

Example:

$ git commit -m "Add new feature"
[master 1234567] Add new feature
 2 files changed, 10 insertions(+)
 create mode 100644 file1.txt
 create mode 100644 file2.txt

5. git push

The git push command is used to upload the local repository changes to a remote repository, such as GitHub or GitLab. This allows other team members to access and collaborate on the project.

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.25 KiB | 1.25 MiB/s, done.
Total 10 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), done.
To https://github.com/user/repository.git
   1234567..7654321  master -> master

6. git pull

The git pull command is used to fetch and merge changes from a remote repository to the local repository. This is useful when you want to update your local codebase with the latest changes made by your team members.

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/user/repository
 * branch            master     -> FETCH_HEAD
Updating 1234567..7654321
Fast-forward
 file1.txt | 2 +-
 file2.txt | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

7. git status

The git status command is used to display the current state of the working directory and the staging area. This command shows which files have been modified, added, or deleted, and which changes have been staged for the next commit.

Example:

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   file1.txt
        new file:   file2.txt

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:   file3.txt

8. git log

The git log command is used to display the commit history of the repository. This command shows the commit messages, authors, dates, and the changes made in each commit.

Example:

$ git log
commit 7654321abcdef (HEAD -> master, origin/master)
Author: John Doe <[email protected]>
Date:   Fri Apr 14 14:30:00 2023 -0400

    Add new feature

commit 1234567fedcba
Author: Jane Smith <[email protected]>
Date:   Wed Apr 12 10:15:00 2023 -0400

    Fix bug in file1.txt

commit 9876543abcdef
Author: Bob Johnson <[email protected]>
Date:   Mon Apr 10 16:45:00 2023 -0400

    Initial commit

These are just a few of the most common Git commands. Git has a rich set of features and commands that can help you manage your project's history, collaborate with others, and streamline your development workflow. As you become more familiar with Git, you'll discover additional commands and techniques that can further enhance your productivity and collaboration.

0 Comments

no data
Be the first to share your comment!