DEV Community

Udara Dananjaya
Udara Dananjaya

Posted on

Mastering Git and GitHub: A Comprehensive Guide to Basic and Advanced Commands

Git is an indispensable tool for modern developers, enabling efficient version control and seamless collaboration. Whether you're working alone or as part of a team, Git and GitHub can help you maintain a clean codebase and avoid common pitfalls. In this article, we'll walk through both basic and advanced Git commands, exploring key concepts such as git commit --amend, git reset, and branching from specific commits to better manage and manipulate Git history.


Table of Contents

  1. Introduction to Git and GitHub
  2. Basic Git Commands
    • git init
    • git clone
    • git add and git commit
    • git push and git pull
    • git status and git log
  3. Advanced Git Commands
    • git commit --amend
    • Changing Commit Dates with --date
    • git reset: Working with Staging Area and History
    • Branching from a Specific Commit
    • git rebase vs. git merge
    • git cherry-pick
    • git reflog
  4. Best Practices for Git Workflow
  5. Conclusion

1. Introduction to Git and GitHub

Git is a distributed version control system that allows you to track changes to files, manage versions of your code, and collaborate with others. Whether you're working on a personal project or collaborating in a team environment, Git helps you efficiently manage code changes and avoid conflicts.

GitHub is a cloud-based Git repository hosting service. It provides an easy-to-use interface for managing Git repositories, collaborating with other developers, and sharing your code with the world. It allows developers to work with Git remotely, offering powerful collaboration features like pull requests, issues, and actions.

Before we dive into the advanced commands, it's essential to understand the basic Git commands, which form the foundation for all Git workflows.


2. Basic Git Commands

git init

To start a new Git repository in your current directory, you use:

git init
Enter fullscreen mode Exit fullscreen mode

This command creates a .git directory that Git uses to track the history of your project. It's the first step when starting a new version-controlled project.

git clone

If you want to clone an existing Git repository (from GitHub or any Git server), use the git clone command:

git clone https://github.com/username/repository.git
Enter fullscreen mode Exit fullscreen mode

This command downloads the entire repository, including its history, to your local machine.

git add and git commit

After making changes to files in your project, you need to stage those changes and commit them to the repository. Here's how:

git add .
git commit -m "Your commit message"
Enter fullscreen mode Exit fullscreen mode

git add . stages all changes (new files, modified files, and deletions), and git commit -m "message" records the changes in the Git history with a descriptive message.

git push and git pull

To synchronize your local repository with a remote one (like on GitHub), you use:

git push origin main
Enter fullscreen mode Exit fullscreen mode

This uploads your local commits to the main branch on GitHub.

To download changes made by others to your local repository, use:

git pull origin main
Enter fullscreen mode Exit fullscreen mode

git status

To see which files have been modified, staged, or committed, use:

git status
Enter fullscreen mode Exit fullscreen mode

This provides a summary of the current state of your working directory and staging area.

git log

To view the commit history, you can use the git log command:

git log
Enter fullscreen mode Exit fullscreen mode

This displays a list of commits, including the commit hash, author, date, and commit message. You can also use various options (like git log --oneline) to modify the log output.


3. Advanced Git Commands

Once you're comfortable with the basics, it's time to explore more advanced Git commands that provide greater control over your repository history and workflow.

git commit --amend: Fixing the Last Commit

Have you ever made a commit and realized you forgot to include some changes or made an error in the commit message? With git commit --amend, you can amend your last commit without creating a new one.

git commit --amend
Enter fullscreen mode Exit fullscreen mode

This opens your text editor with the message of the last commit, allowing you to modify the commit message or add staged changes to it.

Changing the Commit Date

Sometimes, you need to change the timestamp of a commit for consistency or organization purposes. The --date option allows you to modify the commit date:

git commit --amend --date="2024-11-13 14:00:00 +0100" --no-edit
Enter fullscreen mode Exit fullscreen mode

Here, --no-edit retains the previous commit message, and --date sets the timestamp.

git reset: Rewriting Your Commit History

The git reset command is useful for undoing commits and altering your project’s history. It has three main modes: soft, mixed, and hard.

  • git reset --soft <commit>: Resets the HEAD to the specified commit and stages changes (keeping your files intact).
  • git reset --mixed <commit>: Resets the HEAD and unstages the changes (but leaves the working directory unchanged).
  • git reset --hard <commit>: Resets the HEAD and discards all changes in both the staging area and the working directory.

For example, to completely remove commits after a certain point:

git reset --hard <commit_hash>
Enter fullscreen mode Exit fullscreen mode

This moves the HEAD to the specified commit and deletes any changes in your working directory.

Branching from a Specific Commit

There are times when you need to create a new branch from an earlier commit. To do this, use the following command:

git checkout -b new-branch <commit_hash>
Enter fullscreen mode Exit fullscreen mode

This creates a new branch starting from the specified commit, allowing you to continue work from that point.

Getting a Branch's History Like Another Branch

If you need to reset your branch’s history to match that of another branch, you can use git reset:

git reset --hard feature-branch
Enter fullscreen mode Exit fullscreen mode

This resets the current branch’s history to exactly match feature-branch, discarding any changes in the working directory.

git cherry-pick: Applying Specific Commits

If you want to apply a commit from one branch to another, without merging the entire branch, git cherry-pick is your go-to tool:

git cherry-pick <commit_hash>
Enter fullscreen mode Exit fullscreen mode

This takes a specific commit from another branch and applies it to the current branch.

git reflog: Recovering Lost Commits

Sometimes, you may accidentally lose commits or need to recover a branch. git reflog tracks changes to your HEAD and allows you to recover lost references:

git reflog
Enter fullscreen mode Exit fullscreen mode

This shows the history of all changes to the HEAD, including reset operations, merges, and rebase activities. You can use the reflog to restore lost commits.


4. git rebase vs. git merge: Understanding the Difference

Both git rebase and git merge integrate changes from one branch into another, but they operate differently.

  • git merge: Combines two branches by creating a new merge commit, which can result in a more cluttered history.
  • git rebase: Reapplies commits from one branch onto another, creating a linear history by rewriting commit history.

For example, to rebase your current branch onto main:

git checkout feature-branch
git rebase main
Enter fullscreen mode Exit fullscreen mode

While rebase makes history look linear and clean, be cautious when rebasing shared branches, as it rewrites commit history and can lead to confusion in a team setting.


5. Best Practices for Git Workflow

To make the most of Git, adhere to these best practices:

  1. Commit Often: Small, frequent commits are easier to manage than large, infrequent ones.
  2. Write Clear Commit Messages: Your commit message should explain the why behind your changes.
  3. Use Branches for Features: Isolate new work into feature branches to avoid disrupting the main branch.
  4. Rebase for Clean History: Use git rebase to keep your history linear and avoid unnecessary merge commits.
  5. Pull Regularly: Keep your local repository up to date by pulling from the main branch frequently.
  6. Avoid git reset --hard on Shared Branches: When working in teams, avoid using git reset --hard on branches that others are working on.

6. Conclusion

Mastering Git and GitHub commands is essential for any developer, and the advanced commands explored in this article offer deep control over your repository. Whether you're fixing the last commit with git commit --amend, manipulating commit history with git reset, or recovering lost commits using git reflog, these tools will enhance your development workflow and allow you to manage your project with precision and confidence.

By applying these advanced Git techniques in your daily work, you’ll improve your collaboration, keep your project history clean, and handle mistakes with ease.

Happy coding!


Further Reading:

Top comments (0)