How to Use Git Shell Commands?
Last Updated :
15 Jul, 2024
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
Navigate to the Project DirectoryInitialize 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
Initialize a Git RepositoryAdd 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
Add Files to the Staging AreaCommit Changes
Run git commit -m "commit message" to save the staged changes with a descriptive commit message.
git commit -m "commit message"
Output
Commit ChangesView Commit History
Use git log to see a list of past commits with their associated messages and details.
git log
Output
View Commit HistoryCreate Branches
Execute git branch <branch_name> to create a new branch.
git branch <branch_name>
For Example:
Create BranchesSwitch Branches
Use git checkout <branch_name> to switch to a different branch.
git checkout <branch_name>
For Example
Switch BranchesMerge Branches
Run git merge <branch_name> to merge changes from one branch into another.
git merge <branch_name>
For Example
Merge BranchesPush 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
Push Changes to a Remote RepositoryPull 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
Pull Changes from a Remote RepositoryCheck 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
Check the Current StateExplore 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.