Git
How to Run Git Commands in CMD?
Git is an essential tool for managing code versions, collaborating with teams, and keeping track of changes in your project’s files. While Git Bash is popular among developers, Windows Command Prompt (CMD) can also be used to run Git commands effectively. This guide will walk you through how to set up Git in CMD, common commands you can use, and helpful tips for navigating Git from the command line.
Prerequisites: Installing Git on Windows
If you haven’t installed Git on your Windows machine, follow these steps:
- Download Git: Go to the official Git website and download the latest version for Windows.
- Run the Installer: Open the downloaded file, and follow the installation prompts. Ensure you check the option “Add Git to your PATH environment variable” during setup.
- Verify the Installation: Open CMD and type the following command:
git --version
If Git is installed correctly, this command will return the installed version of Git.
Opening CMD to Run Git Commands
To begin, open CMD on your Windows machine:
- Press
Win + R
to open the Run dialog. - Type
cmd
and hitEnter
. - A Command Prompt window will open where you can now start using Git commands.
Basic Git Commands to Get Started in CMD
1. Initialize a New Repository
If you’re starting a new project, you need to initialize a Git repository in your project folder.
git init
This command will create a hidden .git
directory in your project, where Git stores all version history and settings.
2. Clone an Existing Repository
To work with an existing repository on GitHub or GitLab, you’ll need to clone it to your local system.
git clone <repository-url>
For example:
git clone https://github.com/user/repo.git
This command downloads all the files, branches, and history from the remote repository to your computer.
Managing Changes with Git Commands
Once your repository is set up, you can start adding, committing, and tracking changes. Here are some essential commands:
3. Check the Status of Your Repository
The git status
command shows you the current state of your project files and helps you understand which files have been modified, staged, or committed.
git status
4. Add Files to Staging
Before committing changes, you need to add them to the staging area. To add a specific file, use:
git add <file-name>
For example:
git add index.html
To add all modified files to the staging area, use:
git add .
5. Commit Changes
Committing saves your changes to the repository’s history. Each commit requires a message that describes the changes made.
git commit -m "Your commit message"
For example:
git commit -m "Add homepage design"
Working with Branches in CMD
Branches are a powerful way to work on different versions of a project without affecting the main codebase. Here’s how to manage branches using CMD:
6. Create a New Branch
To create a new branch, use:
git branch <branch-name>
For example:
git branch feature-login
7. Switch to Another Branch
To switch to an existing branch, use:
git checkout <branch-name>
For example:
git checkout feature-login
8. Merge Branches
If you’re finished working on a branch and want to incorporate its changes into another branch, you’ll need to merge it.
- First, switch to the branch you want to merge into (e.g.,
main
):
git checkout main
- Then, merge the other branch into it:
git merge feature-login
Synchronizing with Remote Repositories
When collaborating with others, you’ll need to push your changes to a remote repository (like GitHub) or pull changes made by others.
9. Push Changes to a Remote Repository
To push your local commits to a remote repository, use:
git push origin <branch-name>
For example:
git push origin main
This command uploads the changes from your local main
branch to the main
branch on the remote repository.
10. Pull Changes from a Remote Repository
To update your local branch with the latest changes from a remote repository, use:
git pull origin <branch-name>
For example:
git pull origin main
Advanced Git Commands for CMD
Once you’re comfortable with the basics, here are some additional Git commands that can be helpful:
11. Viewing Commit History
To see a history of commits in your repository, use:
git log
This command will display a list of commits, their authors, dates, and messages. You can press q
to exit the log view.
12. Stashing Uncommitted Changes
If you need to temporarily save changes without committing them, use Git’s stash feature:
git stash
This command stores your changes in a “stash” so you can work on another branch or pull updates without losing your work. Later, you can apply these changes with:
git stash apply
13. Checking Differences in Files
To see changes you’ve made in your files compared to the last commit, use:
git diff
This command displays a line-by-line difference between the current state and the last committed state of your files.
Customizing Git in CMD
Set Up Git Configuration
It’s a good idea to set up Git with your name and email address, as these will be associated with your commits.
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
Configure a Default Text Editor
If you want to use a specific text editor (e.g., Notepad or VS Code) for writing commit messages or resolving merge conflicts, configure it with:
git config --global core.editor "code --wait"
Replace "code --wait"
with your preferred editor’s command (e.g., "notepad"
).
Summary
Using Git in CMD is straightforward once you understand the commands and workflow. Here’s a quick recap:
- Initialize and Clone: Set up a new Git repository with
git init
or clone an existing one withgit clone
. - Stage and Commit: Track changes using
git add
, and commit them withgit commit
. - Work with Branches: Create, switch, and merge branches to manage different versions of your project.
- Sync with Remote: Push and pull changes to keep your local repository in sync with a remote repository.
- Advanced Commands: Use
git log
,git stash
, andgit diff
to explore advanced functionalities.