Git
How to Run Git Commands in CMD?
If you’re a developer working with Git, it’s essential to know how to use Git commands in the Command Prompt (CMD) on Windows. Running Git commands in CMD can be a powerful and efficient way to manage version control, allowing you to clone repositories, track changes, push updates, and more directly from the command line.
This blog will take you through the basics of setting up Git in CMD and demonstrate some common Git commands, providing a solid foundation for using Git in your development workflow.
Prerequisites
Before you begin, make sure that:
- Git is installed on your computer: You can download it from Git’s official website if you haven’t already.
- Git is added to your system’s PATH: During the installation, ensure you select the option to add Git to your PATH so that CMD recognizes Git commands.
Step 1: Verify Git Installation in CMD
To confirm that Git is installed and accessible in CMD, open Command Prompt by pressing Win + R
, typing cmd
, and pressing Enter. Once CMD is open, type:
git --version
This command should return the Git version installed on your system, indicating that Git is set up correctly. If it shows an error, check that Git is added to your PATH environment variable or reinstall Git, ensuring you select the PATH option during setup.
Step 2: Basic Git Commands in CMD
Once you’ve confirmed that Git is accessible, you can start using Git commands in CMD. Here are some basic commands to get you started:
1. Initialize a New Git Repository
To create a new Git repository, navigate to your project’s directory and initialize Git:
cd path\to\your\project
git init
This will initialize a new Git repository in your project folder, setting up the necessary .git
folder to track changes.
2. Clone an Existing Repository
To clone an existing repository from GitHub or another Git service, use the git clone
command followed by the repository’s URL:
git clone https://github.com/username/repository.git
This command will download the repository’s contents into a new folder within your current directory.
3. Check the Status of Your Repository
The git status
command allows you to see changes in your working directory, including untracked and modified files:
git status
This command provides an overview of changes that have been made but not yet committed.
4. Stage Changes
To stage changes (prepare them for commit), use git add
:
git add filename
Or, to stage all changes at once:
git add .
This tells Git which files you want to include in the next commit.
5. Commit Changes
Once you’ve staged changes, you can commit them with a message describing the update:
git commit -m "Your commit message"
This saves a snapshot of your changes in the Git history.
6. Push Changes to a Remote Repository
To push your committed changes to a remote repository (e.g., GitHub), use git push
. If this is your first push, you’ll need to specify the remote branch:
git push origin main
This command uploads your commits to the specified remote branch.
7. Pull Changes from a Remote Repository
To sync your local repository with the latest changes from the remote repository, use git pull
:
git pull origin main
This command downloads new commits from the remote branch and integrates them into your local branch.
Step 3: Additional Git Commands in CMD
Once you’re comfortable with the basics, you can try more advanced commands to streamline your Git workflow.
1. Create a New Branch
Branches allow you to work on different parts of your project independently. Use the following command to create a new branch:
git branch branch-name
Then, switch to the new branch with:
git checkout branch-name
Or combine both steps with -b
:
git checkout -b branch-name
2. Merge Branches
Once you’ve completed work in a branch, you can merge it back into the main branch. First, switch to the branch you want to merge into:
git checkout main
Then, merge the other branch into it:
git merge branch-name
3. View Commit History
To see a history of commits, use:
git log
This command shows a detailed log of all commits in your repository. You can press q
to exit the log view.
4. Delete a Branch
To delete a branch after you’ve merged it, use:
git branch -d branch-name
This command helps keep your branch list organized.
Useful Git Shortcuts in CMD
CMD supports several useful keyboard shortcuts that can save you time:
- Use the Tab key for auto-completion: Typing a few characters of a folder name or command and pressing Tab will auto-complete it.
- Arrow keys: Use the up and down arrow keys to scroll through your command history.
- Ctrl + C: Stops the current command if it’s taking too long to execute.
- Ctrl + L: Clears the terminal screen.
Tips for Running Git in CMD
- Use Aliases for Common Commands: You can set up aliases for frequently used Git commands by adding them to your Git configuration. For example, to shorten
git status
togst
:
git config --global alias.gst status
- Configure SSH Keys for Secure Authentication: Instead of entering your credentials each time, configure SSH keys to authenticate securely and efficiently. Generate an SSH key and add it to your GitHub account for streamlined access.
- Leverage Git Bash for Unix Commands: CMD has limited support for Unix-style commands (e.g.,
ls
,rm
). If you need these, consider using Git Bash, which provides a Unix-like terminal for running Git commands on Windows. - Customize CMD Prompt: For a more customized Git experience in CMD, you can use third-party tools like Oh My Posh, which enhance the command line with themes and Git status indicators.
Conclusion
Running Git commands in CMD is a versatile and efficient way to manage your projects, whether you’re a beginner or an experienced developer. By mastering these core Git commands and CMD tips, you’ll be well-equipped to handle version control directly from the command line, allowing you to streamline your development workflow. With practice, using Git in CMD will become second nature, giving you more control and efficiency in managing your codebase.