This guide walk you through how to use git, covering every command you'll need to navigate your way through version control like a pro. Whether you're a newcomer or a seasoned web developer, this post will provide valuable insights to enhance your workflow.
What is Git?
To start things off, let's address the elephant in the room. What exactly is Git? Git is a distributed version control system. Unlike centralized version control systems, Git doesn't rely on a single repository to store all versions of your project. Instead, every developer has a complete copy on their own machine. This makes Git incredibly fast, scalable, and ideal for collaborating with teams of all sizes.
Before diving into the commands, letβs get Git up and running on your system.
Installation
-
Windows:
- Download the Git installer from Git's official website.
- Run the installer, and follow the on-screen prompts. Feel free to use the default settings.
-
macOS:
- Use Homebrew to install Git. Open a terminal and enter:
brew install git
-
Linux:
- Use your package manager. For Ubuntu/Debian, run:
sudo apt-get install git
Configuration
Once installed, it's time to set up your identity. This information will be used in commit messages:
git config --global user.name "Your Name"
git config --global user.email "yourname@example.com"
To confirm your configurations:
git config --list
Starting a Git Repository
Now that you have Git set up, let's start using it.
Initializing a Repository
To create a new Git repository, navigate to your project directory and run:
git init
This command sets up a new repository, creating a .git directory in your project.
Cloning an Existing Repository
If you want to contribute to an existing project, you can clone it to your local machine:
git clone https://github.com/user/repository.git
This creates a copy of the specified repository on your machine.
Basic Commands
With the repository in place, let's explore some basic commands that you'll frequently use.
Checking the Status
To view the status of your working directory and staging area, run:
git status
This command shows which files are staged, modified, or untracked.
Adding Changes
When you make changes to files, you need to add them to the staging area before committing them:
git add filename
To add all files at once:
git add .
Committing Changes
After staging your changes, you'll need to commit them:
git commit -m "Brief message describing the changes"
This command saves your changes in the repository. The message should be concise yet descriptive.
Viewing Commit History
To see the commit history, use:
git log
This command displays all past commits, along with their messages. For a more compact view:
git log --oneline
Branching and Merging
Branching is one of Git's most powerful features, allowing you to work on different tasks simultaneously.
Creating a Branch
To create a new branch:
git branch new-branch
Switching Branches
To switch to another branch:
git checkout new-branch
Merging Branches
Once you're done working on a branch, you can merge it back into the main branch:
git checkout main
git merge new-branch
Deleting a Branch
After merging, you may want to delete the branch:
git branch -d new-branch
Remote Repositories
Collaborating with others requires interacting with remote repositories.
Adding a Remote
To add a remote repository:
git remote add origin https://github.com/user/repository.git
Viewing Remotes
To list your remote repositories:
git remote -v
Pushing Changes
To push your changes to a remote repository:
git push origin branch-name
If it's your first time pushing a branch:
git push -u origin branch-name
Pulling Changes
To update your local repository with changes from a remote repository:
git pull origin branch-name
Advanced Commands
Let's dive deeper into some advanced commands that offer more control over your repository.
Stashing Changes
If you need to switch branches but aren't ready to commit your changes, you can stash them:
git stash
To reapply the stashed changes:
git stash apply
Viewing Diffs
To see changes between commits or working trees:
git diff
Resetting Commits
To undo a commit and move the changes back to the staging area:
git reset HEAD~1
Reverting Commits
To create a new commit that undoes a previous commit:
git revert commit-id
Rebasing Branches
To reapply commits on top of another base tip:
git rebase branch-name
Solving Conflicts
When multiple changes affect the same part of a file, a conflict occurs. Git provides tools to resolve these conflicts.
Conflict Indicators
When a conflict happens, the affected file contains conflict markers:
<<<<<<< HEAD
Your changes
=======
Changes from the other branch
>>>>>>>
You'll need to edit the file to mark the resolved content.
Adding and Committing Resolved Files
After resolving conflicts, mark the file as resolved:
git add filename
Then, commit the resolution:
git commit
Git Workflows
Understanding different workflows can make managing your projects easier.
Feature Branch Workflow
- Create a Branch: Make a new branch for each feature or bug fix.
- Work on the Branch: Commit your changes to this branch.
- Merge: Once done, merge it back into the main branch.
- Delete the Branch: Clean up by deleting the branch after merging.
Forking Workflow
- Fork: Create a copy of the repository on your GitHub account.
- Clone: Clone your forked repository.
- Feature Branch: Follow the feature branch workflow.
- Pull Request: Create a pull request to merge changes from your forked repo into the original repo.
GUI Tools vs. Command Line
While the command line is powerful, GUI tools provide a more visual experience. Some popular Git GUI tools include:
- GitKraken: A feature-rich Git client with an intuitive interface.
- SourceTree: A free Git client that offers detailed views of your repositories.
- GitHub Desktop: Simplifies Git workflows specifically for GitHub repositories.
Conclusion
Using Git effectively can be a game-changer for web developers. From tracking changes to collaborating seamlessly, mastering these commands will significantly enhance your workflow. Remember, practice makes perfect, and with time, navigating Git will become second nature. Happy coding!
By incorporating Git into your development process, you'll not only keep your work organized but also be prepared to tackle any challenges that come your way. How has Git changed your workflow? Share your experiences and any tips you might have in the comments below!
Top comments (9)
well this is for beginners for advance level you must know about
Definitely ! @syedmuhammadaliraza
I hope you also like
List of Free HTML Template
Niraj Narkhede γ» May 10
Good
git log p -1 expalin
Great post! Well done π
Here is also a free ebook in case that anyone wants to learn more:
bobbyiliev / introduction-to-git-and-github-ebook
Free Introduction to Git and GitHub eBook
π‘ Introduction to Git and GitHub
This is an open-source introduction to Git and GitHub guide that will help you learn the basics of version control and start using Git for your SysOps, DevOps, and Dev projects. No matter if you are a DevOps/SysOps engineer, developer, or just a Linux enthusiast, you can use Git to track your code changes and collaborate with other members of your team or open source maintainers.
The guide is suitable for anyone working as a developer, system administrator, or a DevOps engineer and wants to learn the basics of Git, GitHub and version control in general.
π Download
To download a copy of the eBook use one of the following links:
Dark mode
Light mode
ePub
π Chapters
Very helpful thank you.
Thank you !
You could also checkout
List of 75 CSS Resources for UI Developers
Niraj Narkhede γ» May 15
Thank you, this is great!
Thank you @codeveronica
you could checkout my other posts
Getting Started with React-Helmet: A Beginner's Guide
Niraj Narkhede γ» May 10