Version Control with Git: Fundamental Commands for Developers
Introduction
Git is an essential version control system used by DevOps Training worldwide to track changes, collaborate on projects, and maintain code integrity. Whether you’re working solo or as part of a team, understanding key Git commands can streamline your workflow and prevent common pitfalls.
In this guide, we’ll explore the essential Git commands every developer should know.
1. Setting Up Git
Before using Git, configure it with your user information:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
This ensures that all your commits are associated with your identity.
2. Initializing a Repository
To start tracking a new project, initialize a Git repository:
git init
This creates a .git
directory, where Git stores all version control data.
3. Cloning a Repository
To work on an existing project, clone a repository:
git clone <repository-url>
This downloads the project and its entire history onto your local machine.
4. Checking Repository Status
To check which files have been modified, added, or deleted, use:
git status
This command helps track your progress before committing changes.
5. Adding Files to Staging Area
To stage specific files for commit:
git add <file-name>
To stage all changes:
git add .
This prepares files for the next commit.
6. Committing Changes
Once files are staged, commit them with a descriptive message:
git commit -m "Your commit message"
This saves changes locally in the Git history.
7. Viewing Commit History
To see a list of previous commits:
git log
For a concise view:
git log --oneline
8. Branching in Git
To create a new branch:
git branch <branch-name>
To switch to another branch:
git checkout <branch-name>
Alternatively, create and switch to a new branch in one command:
git checkout -b <branch-name>
9. Merging Branches
To merge changes from another branch into the current one:
git merge <branch-name>
Resolve conflicts if necessary and commit the merge.
10. Pushing Changes to a Remote Repository
To upload commits to a remote repository:
git push origin <branch-name>
11. Pulling Latest Changes
To fetch and integrate updates from a remote repository:
git pull origin <branch-name>
12. Undoing Changes
To discard unstaged changes in a file:
git checkout -- <file-name>
To reset the staging area:
git reset HEAD <file-name>
To undo the last commit while keeping changes:
git reset --soft HEAD~1
To permanently remove the last commit:
git reset --hard HEAD~1
13. Stashing Changes
If you need to save changes without committing:
git stash
To apply stashed changes:
git stash apply
To remove the stash after applying:
git stash drop
14. Tagging Releases
To create a new tag:
git tag -a v1.0 -m "Version 1.0"
To push tags to the remote repository:
git push origin --tags
Conclusion
Mastering these Git commands will help you efficiently manage your code, collaborate with teammates, and avoid common pitfalls in version control. Whether you’re a beginner or an experienced developer, understanding Git is an essential skill in modern software development. Keep practicing and experimenting with Git to unlock its full potential!
Comments
Post a Comment