Git is a powerful tool that facilitates seamless version control and collaboration, enabling GitHub activities directly on your computer. Let’s explore some commonly used commands in the industry to work smarter and faster.
Work Fast and Smart: The Git Workflow
Create a Branch
Start by creating a new branch in your project where you can safely experiment and make changes.Add Commits
Save your work incrementally by committing changes.Open a Pull Request
Use pull requests to share your changes and gather feedback, whether from a colleague nearby or a collaborator across the globe.Discuss and Review
Engage in discussions and refine your code based on reviews.Merge and Deploy
Once finalized, merge the changes into the main branch and deploy your code.
Tip for Beginners:
Don’t worry if terms like branch, pull request, or merge feel unfamiliar. We’ll dive deeper into these concepts in another article.
Setting Up Git
Configure Tooling
Set up user information for your repositories:
$ git config --global user.name "[name]" # Set the name for your commits
$ git config --global user.email "[email]" # Set the email for your commits
$ git config --global color.ui auto # Enable colorized output in the terminal
Essential Git Commands
1. Create Repositories
Start a new repository:
$ git init [project-name]
Clone an existing repository:
$ git clone [url]
2. Make Changes
View new or modified files:
$ git status
Compare staged changes to the last commit:
$ git diff --staged
Unstage a file:
git reset [file]
3. Save Fragments
Temporarily save incomplete changes:
$ git stash
View stashed changes:
$ git stash list
4. Group Changes
Create a new branch:
$ git branch [branch-name]
Switch to a branch:
$ git checkout [branch-name]
Merge branches:
$ git merge [branch]
Selectively apply commits:
$ git cherry-pick [commit]
5. Redo Commits
Undo commits while preserving local changes:
$ git reset [commit]
Discard history and revert to a specific commit:
$ git reset --hard [commit]
6. Synchronize Changes
Fetch updates from a remote repository:
$ git fetch [bookmark]
Push local commits to a remote repository:
$ git push [alias] [branch]
Pull changes and integrate them:
$ git pull
Master these commands, and you’ll be well on your way to becoming a Git pro! Hehe :)