Open In App

How to Use Git Shell Commands?

Last Updated : 15 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Git is a powerful version control system that is important for managing source code in modern software development. While there are many graphical user interfaces (GUIs) available for Git, mastering Git shell commands can significantly enhance your productivity and control over your repositories. This article provides an overview of essential Git shell commands and how to use them effectively.

Steps to Use Git Shell Commands

Below is the general workflow when using Git Bash:

Step 1: Open Git Bash

Launch the Git Bash application on the system.

Step 2: Navigate to the Project Directory

Use the cd command to change the current working directory to the location of the project.

cd path/to/your/project

For Example

Screenshot-2024-07-12-205146
Navigate to the Project Directory

Initialize a Git Repository

If it isn't done already, run git init to create a new Git repository in the project directory.

git init

Output

Screenshot-2024-07-12-205435
Initialize a Git Repository

Add Files to the Staging Area

Use git add <file_name> to add specific files, or git add . to add all changes in the current directory to the staging area.

git add <file_name>
# or
git add .

Output

Screenshot-2024-07-12-205554
Add Files to the Staging Area

Commit Changes

Run git commit -m "commit message" to save the staged changes with a descriptive commit message.

git commit -m "commit message"

Output

Screenshot-2024-07-12-205711
Commit Changes

View Commit History

Use git log to see a list of past commits with their associated messages and details.

git log

Output

Screenshot-2024-07-12-205801
View Commit History

Create Branches

Execute git branch <branch_name> to create a new branch.

git branch <branch_name>

For Example:

Screenshot-2024-07-12-205923
Create Branches

Switch Branches

Use git checkout <branch_name> to switch to a different branch.

git checkout <branch_name>

For Example

Screenshot-2024-07-12-210020
Switch Branches

Merge Branches

Run git merge <branch_name> to merge changes from one branch into another.

git merge <branch_name>

For Example

Screenshot-2024-07-12-210629
Merge Branches

Push Changes to a Remote Repository

If we're collaborating with others, use git push <remote_name> <branch_name> to upload our commits to a remote repository (e.g., on GitHub or GitLab).

git push <remote_name> <branch_name>

For Example

Screenshot-2024-07-12-211504
Push Changes to a Remote Repository

Pull Changes from a Remote Repository

Run git pull <remote_name> <branch_name> to download and integrate changes from the remote repository into the local branch.

git pull <remote_name> <branch_name>

For Example

Screenshot-2024-07-12-211813
Pull Changes from a Remote Repository

Check the Current State

Use git status to check the current state of the repository and see which files have been modified or staged.

git status

Output

Screenshot-2024-07-12-212013
Check the Current State

Explore other Git commands like git rebase, git stash, git reset, and git tag for more advanced operations.

Note: To install Git Bash see this.

Conclusion

In conclusion, git shell commands provide a powerful and flexible way to interact with the Git version control system. Using this, we can effectively manage our codebase, collaborate with others, and maintain a clear history of our project's development.


Next Article
Article Tags :

Similar Reads

three90RightbarBannerImg