Unlock the power of version control with this comprehensive guide to essential Git commands. Learn how to initialize a new Git repository, clone projects, stage changes, and commit with ease. Explore the world of branches, merging, and navigating your project's history effortlessly. Discover the simplicity of checking status, viewing logs, and reverting changes when needed. Whether you're a novice or looking to enhance your skills, this beginner-friendly article covers Git basics with clear examples. Dive into the Git universe and streamline your development workflow today!
Setting Global Git Username and Email
The global git username and email address are associated with the commits on all repositories on your system that don’t have repository-specific values. To set your global commit name and email address, run the git config command with the --global option:
git config --global user.name "Your Name" git config --global user.email "youremail@yourdomain.com"
Beginning Your Project
Initialize a new Git repository.
git init
Clone a remote repository to your local machine.
git clone <repository-url>
Tracking Changes:
Show the status of changes in the working directory.
git status
Stage changes in a specific file for the next commit.
git add <file>
Commit staged changes with a descriptive message.
git commit -m "<message>"
Fetch changes from a remote repository and merges them into the current branch.
git pull origin <branch-name>
Push local changes to a remote repository.
git push <branch-name>
Display a detailed commit history.
git log
Branching and Merging
List available branches in the repository.
git branch
Create a new branch for exploring ideas without disrupting your main codebase, a safe space for experimentation.
git branch <branch-name>>
Switch to a different branch.
git checkout <branch-name>
Merge changes from a specified branch into the current branch.
git merge <branch-name>
Unstage changes for a specific file, keeping modifications in the working directory.
git reset <file>
Create a new commit to reverse changes made in a specified commit.
git revert <commit>
Collaborating with Others
Display a concise one-line commit history.
git log --oneline
Download changes from a remote repository without merging.
git fetch
View the names and URLs of remote repositories.
git remote -v
Add a new remote repository with a specified name and URL.
git remote add <name> <repository-url>
Force-push changes to a remote repository, overwriting its history.
git push --force
Show the differences between the working directory and the last commit.
git diff
Display the patch representing changes for each commit.
git log -p
Mastering these essential Git commands is a pivotal step toward becoming a proficient developer. By understanding the fundamentals of version control, you've equipped yourself with the tools for efficient project management and collaborative coding. From initializing a new repository to navigating branches, tracking changes, and collaborating seamlessly, these Git commands form the backbone of a developer's workflow. Remember, the power of version control lies not just in the commands, but in how you integrate them into your daily coding practices. So, go ahead, experiment, and confidently elevate your development journey. Happy coding!
📚 To Learn How to install Git easily on Windows you can read this article 🚀🚀 Easy Git Installation on Windows: Step-by-Step Guide for Beginners