DEV Community

Cover image for Essential Git Commands: A Developer’s Guide
Satyakoidala
Satyakoidala

Posted on • Updated on

Essential Git Commands: A Developer’s Guide

Introduction

Welcome, fellow developers! In today’s post, we’ll delve into the powerful world of Git commands. Whether you’re managing version control, collaborating with a team, or ensuring code integrity, Git remains an indispensable tool in our arsenal.

Setting Up Your Environment

Assuming you have a Git repository hosted remotely (such as on GitHub), let’s explore the essential commands. Here’s the basic structure of your repository:

/origin (remote origin)
   |-- <master/develop/main branch>
Enter fullscreen mode Exit fullscreen mode

Cloning the Repository

To get started, clone the remote repository to your local machine. GitHub provides two secure protocols for this purpose:

HTTPS Protocol: Authenticate using your Git login credentials.
SSH Protocol: Utilize SSH keys for secure authentication.

# Clone the repository
git clone <remote_repository_link>
Enter fullscreen mode Exit fullscreen mode

Creating and Switching Branches

Stay up-to-date with the remote head by creating a local branch that tracks it:

# Create a new local branch
git checkout -b <new_local_branch> <upstream/remote_head_branch>

# Switch to a specific branch
git checkout <branch_name>

# List available branches
git branch
Enter fullscreen mode Exit fullscreen mode

Keeping Your Workspace Fresh

Before starting work, ensure a clean slate by pulling the latest changes from the remote repository:

# Pull changes from the remote
git pull origin <remote_head_branch>
Enter fullscreen mode Exit fullscreen mode

Committing Your Work

As a developer, wrapping up your day with a git commit is a habit. First, stage your changes:

# Add changes to the staging area
git add "<filename1>" "<filename2>"

# Or add all modified files
git add .
Enter fullscreen mode Exit fullscreen mode

Next, commit your changes:

# Commit with a descriptive message
git commit -m "__commit__msg__"
Enter fullscreen mode Exit fullscreen mode

You can stack any number of commits before you push into the remote origin and create a Pull Request (PR).

Pushing to the Remote Repository

When your task is complete, push your commits to the remote repository:

# Push to the remote
git push origin <working_branchname>
Enter fullscreen mode Exit fullscreen mode

It returns a PR-initiating link, you can use it to create your PR.

Undoing a Commit

Mistakes happen! To undo a commit:

# Undo the last commit (or nth last commit)
git reset HEAD~[1,n]

# Keep changes staged while undoing the commit
git reset --soft HEAD~[1,n]

# Delete the entire commit (use with caution)
git reset --hard HEAD~[1,n]
Enter fullscreen mode Exit fullscreen mode

Comparing Different Heads

Git makes it easy to compare commits, branches, or changes. Learn more in this blog.

# Compare the last commit with uncommitted changes
git diff

# Compare a specific file
git diff -- <filename>

# Compare between branches or commits
git diff <name/hash1>..<name/hash2>
Enter fullscreen mode Exit fullscreen mode

Copying Commits with Cherry-Pick

Copying commits from one branch to another is a common task. The git cherry-pick command allows you to selectively apply specific commits to a different branch. Here’s how:

# Make sure you're on the target branch
git checkout <target_branch>

# Cherry-pick the desired commit
git cherry-pick <to_be_copied_commit_hash>
Enter fullscreen mode Exit fullscreen mode

Checking Branch Status

To assess the status of your local branch relative to its upstream (usually the remote head), use:

# Check branch status
git status
Enter fullscreen mode Exit fullscreen mode

Exploring Commit History

Understanding your git commit history is essential. To view the complete log of commits in reverse chronological order, use:

# View commit log
git log
Enter fullscreen mode Exit fullscreen mode

Deleting a Branch

If the branch is no longer needed! Delete it!

# Delete a branch
git branch -D <branchname>
Enter fullscreen mode Exit fullscreen mode

Conclusion

These commands form the backbone of Git usage. While minor syntactic differences may exist across Git versions, mastering these essentials will serve you well throughout your development journey.

Happy coding!

Feel free to customize this guide to suit your specific needs. If you have any questions or need further assistance, don’t hesitate to ask!

Top comments (2)

Collapse
 
shaurgaleeb profile image
shaurgaleeb

Awesome content

Collapse
 
trilok2703 profile image
Trilok

Useful content.