Git 101:The Most Used Git Commands at a Glance

Git 101:The Most Used Git Commands at a Glance

Important git commands and best practices to follow for Git branching

What is Git?

Git is a version control system that enables developers to track changes to their code and collaborate with others on software projects. Using Git, developers can create and manage repositories, which are collections of files and directories that are stored in a central location. They can also create branches, which are separate versions of the codebase that can be worked on independently. This allows developers to experiment with new features or fix bugs without affecting the main codebase.

Git common commands

Here is a list of some common Git commands📜

  1. git clone: Clone a remote repository to your local machine.

  2. git init: Initialize a new Git repository.

  3. git add: Add one or more files to staging.

  4. git commit: Commit staged changes with a commit message.

  5. git push: Push committed changes to a remote repository.

  6. git pull: Pull changes from a remote repository to your local machine.

  7. git branch: Create, list, or delete branches.

  8. git checkout: Check out a branch or restore working tree files.

  9. git merge: Merge branches.

  10. git status: Check the status of your repository.

These are just a few of the many Git commands available.🐝 For a complete list of Git commands and more detailed explanations of each one, you can refer to the Git documentation https://git-scm.com/doc.

Let's understand more about branches and see a few commands used for the git branch.

What is a Git branch?

In Git, a branch is a separate version of the codebase that can be worked on independently of the main codebase. Branches allow developers to experiment with new features or fix bugs without affecting the main codebase, which is usually referred to as the master branch.

Git Branches: List, Create, Switch to, Merge, Push, & Delete

Any changes made to the branch are separate from the main codebase and are not reflected in the master branch until the changes are merged back into it. This allows developers to work on multiple features or fixes 🔧 simultaneously without worrying about affecting the main codebase.

For example, a developer might create a branch for each new feature they are working on, or a team might use branches to separate work on different parts of the codebase. 👉🏻Git provides tools for creating, listing, and deleting branches, as well as for reviewing and merging changes made on different branches. This makes it easier to collaborate on projects and ensures that the code is always in a stable state🧮

It allows developers to save versions of their code, revert back to previous versions if necessary, and collaborate with others by sharing and merging code changes. In this article, we'll cover some of the most common Git commands for working with branches.

  1. git branch: This command lists all of the branches in the current repository. By default, the current branch is indicated with an asterisk.

  2. git branch <branch-name>: This command creates a new branch with the specified name.

  3. git checkout <branch-name>: This command switches to the specified branch. Any changes made to the code will be made to this branch until you switch to a different branch.

  4. git merge <branch-name>: This command merges the specified branch into the current branch. This is often used to incorporate changes made in a separate branch into the main branch of the repository.

  5. git branch -d <branch-name>: This command deletes the specified branch. Be careful when using this command, as it permanently removes the branch and any changes made in that branch will be lost.

  6. git stash: This command temporarily stores changes that have been made to the code but have not been committed. This can be useful when you need to switch branches but don't want to commit your changes yet.

Git Branching Best Practices

💁🏿‍♂️Here are a few best practices for working with Git branches:

  1. Create a new branch for each new feature or bug fix: This helps to keep your work organized and makes it easier to track changes.

  2. Use descriptive branch names: Use names that clearly describe the work being done on the branch. This will make it easier to understand the purpose of each branch and identify which branch to merge back into the main branch.

  3. Keep the main branch clean: The main branch (usually called master) should contain only stable, production-ready code. Avoid making changes directly to the main branch, and instead, create a new branch for each new feature or bug fix.

  4. Merge branches frequently: Regularly merging changes from other branches into your main branch helps to keep your codebase up-to-date and reduces the risk of conflicts when you need to merge your changes back into the main branch.

  5. Use a branching strategy: A branching strategy is a set of guidelines for how branches should be used in a project. There are many different branching strategies, but some common ones include Gitflow and GitHub flow.

By following these best practices, you can effectively manage📎 your codebase and collaborate with others using Git branches.

📑This was a short cheatsheet for most common git commands. In the next blog, I'll write about git installation and configuration process and also show a practical demonstration of git commands for any open-source network.