Essential Git Commands: A Developer’s Guide
Tags: git, github
Published: 2024-04-21 12:37:47

## 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>
```

## 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>
```

## 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
```

## 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>
```

## 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 .
```

Next, commit your changes:
```
# Commit with a descriptive message
git commit -m "__commit__msg__"
```
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>
```
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]
```

## Comparing Different Heads
Git makes it easy to compare commits, branches, or changes. Learn more in this [blog](https://www.git-tower.com/learn/git/faq/git-compare-two-branches).
```
# 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>
```

## 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>
```

## 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
```

## 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
```

## Deleting a Branch
If the branch is no longer needed! Delete it!
```
# Delete a branch
git branch -D <branchname>
```

## 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!