Essential Git Commands for Beginners

Rehan Pinjari
5 min readJul 16, 2024

--

Okay, let’s talk about Git! Not the creepy crawly thing, but the amazing tool that saves a coder’s life.

If you’re new to the coding world, Git can seem to be a huge, twisted ball of string.

But don’t worry; it’s your new best buddy. Think of Git as a time machine for your code.

You can go back in time, correct troublesome errors, and work with other developers without losing your head.

It’s like having powers in your code! And trust me, you will thank yourself in the future for knowing this.

I’ll break down Git into easy-to-understand pieces, with enough of examples and interesting details to keep you engaged.

So grab a coffee (or your favorite coding drink) and let’s get started! Ready?

Core Git Commands

  • Creating a new Git repository
git init

Consider this command to be like building up a comfortable little house for your code.

You are basically saying to your computer, “Hey, I want to keep track of every change I make to this project.”

It’s like making a memory book for your code!

  • Cloning an existing repository
git clone https://github.com/username/repo.git

Want to get all of the code and history for your team’s project? Simply run git clone followed by the project’s site address. It’s like downloading a complete copy of everything onto your computer. Easy peasy!

Basic Workflow

  • Checking the repository status
git status

Feeling lost in your code jungle? Do not panic! git status is your trustworthy compass. It’ll offer you a clear view of what’s going on in your repository right now.

  • Staging changes for commit
git add .

Are you ready to save your progress? To stage changes, use git add. It's like saying, "Hey Git, take a look at this!".

  • Committing changes to the local repository
git commit -m "Add awesome new feature"

Use git commit to seal the deal. It’s like starting a diary entry on what you did.

  • Viewing commit history
git log

Curious about history? git log shows the history of your commits. Time travel, anyone?

  • Comparing changes between commits or working directory
git diff

Want to see what’s changed? git diff is like a before-and-after comparison.

Branching and Merging

  • Creating, listing, and deleting branches
git branch new-feature

Branches are like alternate realities. Create one with git branch and explore new features without affecting the main timeline.

  • Switching between branches
git checkout new-feature

git checkout lets you move between branches.

  • Merging branches
git merge new-feature

Have you found the perfect feature? Use git merge to bring it back to the main timeline.

  • Rebase one branch onto another
git rebase main

Rebasing is like rearranging history. Use it to keep your commits clean and linear.

Remote Repositories

  • Managing remote repositories
git remote add origin https://github.com/username/repo.git

git remote connects your local repository to a remote one. It’s similar to linking your phone with the cloud.

  • Pushing changes to a remote repository
git push origin main

git push will send your commits to the remote repository. It’s similar to transferring your stuff to the cloud.

  • Fetching and merging changes from a remote repository
git pull origin main

Keep up to date using git pull. It’s like getting the newest updates from the cloud.

Advanced Git Commands

Undoing Changes

  • Undoing the last commit or unstaging changes
git reset --hard HEAD~1

Made a mistake? git reset is like an undo button for your commits.

  • Reversing a commit without modifying history
git revert <commit_hash>

Need to go back without changing history? git revert is your buddy.

  • Discarding changes in the working directory
git checkout -- <file>

Regret that last change? Use git checkout -- to discard uncommitted changes.

Finding and Fixing Issues

  • Showing who last modified a line of code
git blame <file>

Pointing fingers? git blame shows who changed what and when.

  • Finding the commit that introduced a bug
git bisect start

Hunting a bug? git bisect helps you find the exact commit that caused it.

  • Applying a single commit from one branch to another
git cherry-pick <commit_hash>

Want a specific change? git cherry-pick lets you apply a commit from one branch to another.

Collaboration and Workflow

  • Temporarily saving changes
git stash

Need to switch tasks? git stash saves your changes temporarily.

  • Creating and managing tags
git tag v1.0

Mark important points in your project’s history with git tag.

  • Including other Git repositories as submodules
git submodule add https://github.com/username/repo.git

Managing a large project? Use git submodule to include other repositories.

Best Practices and Tips

Importance of clear and descriptive commit messages

Think of your commit messages as diary entries. Clear and descriptive messages make it easier to understand the history of your project.

Using branches effectively for feature development and bug fixes

Branches are your best friends for keeping new features and bug fixes separate from the main project.

Keeping your repository clean and organized

Regularly clear up old branches and use.Use gitignore to keep your repository clean.

Resolving merge conflicts

Don’t panic if you find a merge conflict. Review the changes made to figure out which ones to keep.

Tips for most effective Git usage.

  • Commit messages: Keep them short, however informative.
  • Branching strategies: Choose a branching strategy that fits your workflow, such as Git Flow or GitHub Flow.
  • Using tags and releases: Tag important milestones and build releases for versioned deployments.

Troubleshooting

Common challenges along with solutions

Got stuck? Here are two common Git issues and approaches to resolve them:

  • Merge conflicts: Resolve conflicts manually and then commit.
  • Detached HEAD: Reattach with git checkout main.

Useful Git commands for troubleshooting

  • git log: View the commit history.
  • git diff: Compare the changes made.
  • git bisect: Find the commit that created the bug.

Final Words

So there you have it! Git might seem scary at first, but with these basic commands and a little experience, you’ll be managing your projects like a pro.

Remember that Git is here to make your life simpler, so use its power and let it help you become an even better web developer.

Happy coding!

--

--

Rehan Pinjari
Rehan Pinjari

Written by Rehan Pinjari

Self-Taught Developer & UI/UX Designer

Responses (1)