Skip to main content

Command Palette

Search for a command to run...

Essential Git Commands for Every Developer

Published
7 min read
D

Hi, I’m Deepika 👋 I’m a 3rd year B.Tech Information Technology student and an aspiring Full-Stack Developer with a strong interest in building clean, user-friendly web applications. I enjoy working with React, Flask, Firebase, and SQLite, and I’m currently focused on strengthening my frontend fundamentals through a 3-month sprint covering HTML, CSS, Tailwind CSS, and responsive UI design. I write about my learning journey, frontend concepts, and small projects here on Hashnode. I also share my source code and daily practice on GitHub as part of my #BuildInPublic approach. I’m open to internships, collaborations, and learning opportunities. Let’s connect and grow together 🚀

In the previous blogs, we explored the basics of GitHub from version control to creating a new repository. Now let’s wrap up the series by diving into some essential git commands that help you to manage your code and repositories.

Let’s recap some basic terms:

  • Directory: Folder

  • Terminal or Command-line: Interface for text commands

  • CLI: Command line interface

  • CD: Change Directory

  • Code Editor: Word processor for writing code

  • Repository: Project or the folder/place where your project is kept.

  • Git: The tool tracks the change in your code over time.

  • GitHub : It is a website where you host all of your Git repositories.

Essential Git Commands:

  1. git init: Initializes a new Git repository in the current directory.

     git init
    
  2. git clone [url]: Clones a repository from a remote URL to your local machine.

     git clone https://github.com/user/repo.git
    
  3. git add [file]: Adds a file to the staging area.

     git add filename
    
  4. git commit -m "[message]": Commits the staged changes with a descriptive message.

     git commit -m "Initial commit"
    
  5. git status: Shows the status of changes as untracked, modified, or staged.

     git status
    
  6. git log: Displays the commit history.

     git log
    
  7. git branch: Lists all the branches in your repository. The current branch will be highlighted with an asterisk.

     git branch
    
  8. git checkout [branch]: Switches to the specified branch and updates the working directory.

     git checkout feature-branch
    
  9. git merge [branch]: Merges the specified branch into the current branch.

     git merge feature-branch
    
  10. git pull: Fetches and integrates changes from the remote repository to the current branch.

    git pull origin main
    
  11. git push: Pushes the local branch commits to the remote repository.

    git push origin main
    
  12. git remote -v: Displays the URLs of the remote repositories.

    git remote -v
    
  13. git fetch: Downloads objects and refs from another repository.

    git fetch origin
    
  14. git diff: Shows the differences between the working directory and the staging area.

    git diff
    
  15. git reset [file]: Removes the specified file from the staging area but keeps the file changes in the working directory.

    git reset filename
    
  16. git rm [file]: Removes a file from the working directory and stages the removal.

    git rm filename
    

These commands form the foundation of working with Git and will help you manage your code efficiently.

After mastering the essential Git commands, it's important to understand how to work with forks. Forking is a powerful feature in GitHub that allows you to create a personal copy of someone else's repository. This is particularly useful for contributing to open-source projects or managing your own version of a project. Let’s explore the steps and commands involved in forking a repository and keeping it up-to-date with original source.

Forking a Repository:

  1. Fork a Repository on GitHub:

    • Navigate to the repository you want to fork on GitHub.

    • Click the "Fork" button at the top right of the repository page.

    • This will create a copy of the repository under your GitHub account.

  2. Clone Your Forked Repository:

    • After forking, you need to clone your forked repository to your local machine.

    • Use the following command:

    git clone [your-forked-repo-url]
  1. Add Upstream Remote:

    • To keep your fork up-to-date with the original repository, you need to add the original repository as an upstream remote.

    • Use the following command:

    git remote add upstream [original-repo-url]
  1. Fetch Upstream Changes:

    • To fetch the latest changes from the original repository, use:
    git fetch upstream
  1. Merge Upstream Changes:

    • To merge the fetched changes into your local branch, use:
    git merge upstream/main
  • Replace main with the appropriate branch name if it's different.
  1. Push Changes to Your Fork:

    • After making changes and committing them to your local repository, push them to your forked repository on GitHub:
    git push origin [your-branch-name]

These steps and commands will help you manage your forked repositories effectively.

After understanding the essential Git commands and how to work with forks, it's crucial to learn about merge conflicts. Merge conflicts occur when Git encounters conflicting changes in the code that it cannot automatically resolve. This often happens when multiple collaborators make changes to the same lines of a file or when one branch modifies a file that has been deleted in another branch. Knowing how to handle merge conflicts effectively is essential for maintaining a smooth workflow and ensuring that your project remains consistent and error-free. Let's delve into the concept of merge conflicts and explore the steps to resolve them.

Understanding Merge Conflicts

  1. Detection:

    • Git will notify you of a merge conflict when you attempt to merge branches. The conflicting files will be marked, and you will need to resolve these conflicts manually.
  2. Conflict Markers:

    • Git uses conflict markers to indicate the conflicting sections in the files. These markers look like this:
    <<<<<<< HEAD
    // Your changes
    =======
    // Changes from the branch you are merging
    >>>>>>> branch-name

Resolving Merge Conflicts

  1. Identify Conflicts:

    • Open the conflicting files in your code editor. Look for the conflict markers (<<<<<<<, =======, >>>>>>>).
  2. Resolve Conflicts:

    • Decide which changes to keep. You can choose one side, combine changes, or make entirely new changes.

    • Remove the conflict markers after resolving the conflicts.

  3. Stage Resolved Files:

    • After resolving the conflicts, stage the resolved files using:
    git add [file]
  1. Commit the Merge:

    • Commit the resolved changes to complete the merge:
    git commit

Example Workflow

  1. Attempt to Merge:

     git merge [branch-name]
    
  2. Resolve Conflicts:

    • Open the conflicting files and resolve the conflicts manually.
  3. Stage and Commit:

     git add [resolved-file]
     git commit
    

Tools for Resolving Conflicts

  • Code Editors: Many code editors like VSCode, Atom, and Sublime Text have built-in tools to help visualize and resolve merge conflicts.

  • Merge Tools: Dedicated merge tools like KDiff3, Meld, and Beyond Compare can also be used to resolve conflicts more efficiently.

Best Practices

  • Frequent Pulls: Regularly pull changes from the remote repository to minimize the chances of conflicts.

  • Small Commits: Make small, frequent commits to make conflict resolution easier.

  • Clear Communication: Communicate with your team to coordinate changes and avoid conflicts.

Understanding and resolving merge conflicts is a crucial skill for effective collaboration in Git. By following these steps and best practices, you can handle merge conflicts smoothly and maintain a clean project history.

In conclusion, mastering Git and GitHub is essential for any developer looking to manage their code efficiently and collaborate effectively with others. From understanding the basics of version control and creating repositories to learning essential Git commands, forking repositories, and resolving merge conflicts, this series has covered the fundamental aspects of using Git and GitHub. By applying these concepts and best practices, you can ensure a smooth and productive workflow, maintain a clean project history, and contribute meaningfully to both personal and open-source projects. Keep practicing and exploring more advanced features to further enhance your skills and streamline your development process. Happy coding!

Further Reading for Advanced Concepts

To deepen your understanding and explore more advanced Git and GitHub concepts, consider the following resources:

  1. Pro Git by Scott Chacon and Ben Straub:

    • This book is available for free online and covers both basic and advanced Git concepts.

    • Pro Git Book

  2. GitHub Learning Lab:

    • GitHub offers interactive courses to help you learn and practice advanced GitHub features.

    • GitHub Learning Lab

  3. Atlassian Git Tutorials:

    • Atlassian provides comprehensive tutorials on various Git topics, including branching strategies, rebasing, and more.

    • Atlassian Git Tutorials

  4. GitHub Docs:

    • The official GitHub documentation is a great resource for learning about advanced GitHub features and workflows.

    • GitHub Docs

  5. GitHub Actions:

    • Learn about GitHub Actions to automate your workflows directly in your GitHub repository.

    • GitHub Actions

  6. Advanced Git Tutorials by GitKraken:

    • GitKraken offers tutorials and webinars on advanced Git topics and best practices.

    • GitKraken Tutorials

These resources will help you build on the foundational knowledge covered in this series and take your Git and GitHub skills to the next level.