Essential Git Commands Every Developer Should Know

Category: Software Install and Setup

Git is an essential tool for developers, enabling efficient version control, collaboration, and project management. Whether you're working on a solo project or contributing to a large team, mastering Git commands will make your workflow more effective. In this guide, we’ll explore the most important Git commands every developer should know.

1. Setting Up Git

Before using Git, you need to configure it with your username and email:

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

To check your Git configuration, use:

git config --list

2. Creating and Cloning Repositories

To start a new Git repository in a directory, run:

git init

To clone an existing repository from GitHub or another remote source:

git clone https://github.com/user/repository.git

3. Checking Repository Status

To check the status of your repository, including untracked and modified files:

git status

4. Adding and Committing Changes

To stage all modified and new files:

git add .

To commit changes with a meaningful message:

git commit -m "Initial commit"

5. Working with Branches

To create a new branch:

git branch new-feature

To switch to a different branch:

git checkout new-feature

To create and switch to a new branch in one step:

git checkout -b new-feature

6. Merging Branches

To merge a branch into the main branch:

git checkout main
git merge new-feature

7. Pushing and Pulling Changes

To push your local changes to a remote repository:

git push origin main

To pull the latest changes from a remote repository:

git pull origin main

8. Undoing Changes

To reset a file to its last committed state:

git checkout -- filename

To undo the last commit:

git reset --soft HEAD~1

9. Viewing Commit History

To see the commit history of your repository:

git log

For a concise, one-line summary of commits:

git log --oneline

10. Stashing Changes

If you need to temporarily save changes without committing:

git stash

To retrieve stashed changes:

git stash pop

Conclusion

Mastering Git is essential for efficient development workflows. These essential Git commands will help you track changes, collaborate with teams, and manage projects with ease. To learn more, check out the official Git documentation.

Tags: