Git &GitHub - The commands and features you must know

Raju GowdaRaju Gowda
6 min read

Git is a distributed version control system that allows developers to track code changes and collaborate effectively with other developers. Git is designed to make the development process fast, flexible, and efficient, allowing multiple developers to work on a project simultaneously without conflicts.

Key Features:

  1. Local and Global Repositories:
    The repository is a codebase maintained by Git to track changes. There are two types of repositories: Local Repository and Global Repository.

    • Global Repository:
      A global repository, also known as a remote repository, is hosted on a server and is accessible to all team members. It acts as the central source of truth for the project, containing the official version of the codebase.

    • Local Repository:
      A local repository is stored on the developer's computer, usually a copy of the global repository. Developers can make changes, work on new issues in their local system, and then merge to the global repository.

  2. Distributed Development:
    Git is a distributed version control system, allowing each developer to have a local copy of the global repository. This facilitates developers to collaborate and work together on different aspects of the same application simultaneously.

  3. Branching and Merging:
    Git allows developers to create copies of the root project via Branching, enabling them to work on features and bug fixes independently and then merge to the root branch.

  4. Staging:
    Staging is selecting the changes that must be committed (saved) to the local repository. Git introduces a staging area where changes can be selectively added before committing. This allows for more controlled and organized commits.

  5. History Tracking:
    Git keeps a detailed history of changes, including who made them, when they were made, and the commit messages. This helps in understanding the evolution of the codebase.

  6. Open Source
    Git is a software tool that is available for free as an open-source. This means that anyone can access and modify its source code. Due to this, there is a large community of developers who actively contribute to different projects.

Git Commands

  1. git init

    Initializes the new git local repository in the project folder, and helps record the changes to the codebase.

  2. git add [file-name]

    Stages the specified files. multiple file paths can be specified

  3. git commit -m "message"

    Records the staged changes in the local Git repository and prepares them for pushing to the remote repository. "-m" is a flag used to specify the message for the respective commit.

  4. git checkout -b [new-branch-name]
    Creates a new branch with the specified name and switches to that branch. The new branch is essentially a copy of the current branch (the branch you were on when you executed the command).

  5. git checkout [existing-branch-name] (or) git switch [existing-branch-name]
    Switches to the specified branch from the current branch.

  6. git log
    Prints the commits made to the current branch.

  7. git log [branch-name]
    Prints the commits made to the specified branch.

  8. git log --oneline
    Concise way to view the commit history in a Git repository. When you execute this command in your terminal within a Git repository, it displays a list of commits in a condensed format, showing only the commit hash (the unique identifier for each commit) and the commit message on a single line.

  9. git branch
    Prints all the local branches present.

  10. git branch -a
    Prints all the global and local branches present.

  11. git show [commit-hash]
    Views the committed changes in the specified commit hash of the commit.

  12. git checkout [commit-hash]

    Used to checkout to a specific commit using its commit hash. This allows you to view the state of the repository at that particular point in time
    Note: When you check out a specific commit by its hash, you enter a "Detached HEAD" state. This means that you're not on any branch. If you make new commits in this state, they will not be part of any branch.

  13. git checkout -b [new-branch-name] [commit-hash]

    If you want to make changes based on that commit and keep them, you can create a new branch from that commit. This command creates a new branch new-branch-name starting from commit [commit-hash] and checks it out.

  14. git remote add origin [remote-repository-path]
    Links the remote repository to the global repository.

  15. git remote set-url origin [new-remote-repository-path]

    Resets the remote repository URL path to the specified new path

  16. git push origin [branch-name]
    Pushes the code to the branch with the specified name in the global repository. If the global repository does not have a branch with the same name as the current branch of the local repository, a new branch is created.

  17. git pull origin [branch-name]
    Pulls the code from the specified branch name.

  18. git clone [remote-repository-path]
    Clones the global repository to the local machine as a local repository.

  19. git remote -v

    This will show you the current URL(s) associated with your remote(s), typically named origin.

  20. git revert [commit hash]
    Helps to revert your changes and go back to the previous state in your Git branch.

๐Ÿ’ก
While using this command, you might be prompted to input a message explaining the revert. To do so, press i to enter insert mode in the editor, type your message, then save and exit by typing :wq. Following these steps ensures a smooth and effective reversal of changes in your Git repository.
๐Ÿ’ก
If you try to revert to a commit that isn't the current head, Git will attempt to revert its previous commit. This might result in a conflict that needs to be resolved.

GitHub

GitHub is a cloud-based platform that allows you to create repositories globally and synchronize them with local repositories. It facilitates features such as creating pull requests, merging, branching, collaborating, and access control with a user interface.

Pull Requests

A pull request is a mechanism for submitting contributions to a codebase. It is an action of proposing the changes to the code owners which are later reviewed and merged into the master branch.

Merging

The process of combining the changes from one branch to another branch is called merging. When the developer opens the pull request, the code is reviewed by the code owners or team members, once the code is verified for its correctness, the code is merged with the requested branch.

Branch Rules

Branch rule is a feature that GitHub provides that helps code owners add rules to their branches to avoid illegal merges.

Conclusion

Git is a powerful and widely used distributed version control system with key features such as distributed development, branching and merging, staging, history tracking, and open-source availability. It allows developers to work collaboratively on projects, manage code changes efficiently, and maintain a detailed history of the codebase evolution.

GitHub complements Git by providing a cloud-based platform for hosting repositories globally and facilitating collaboration through features like pull requests, merging, branching, and access control. Pull requests serve as a mechanism for proposing and reviewing code changes before merging them into the master branch. Merging is the process of combining changes from one branch to another, and GitHub provides tools like branch rules to enforce code quality and avoid illegal merges.

In summary, Git, along with GitHub, offers a comprehensive solution for version control and collaborative development, empowering developers to work efficiently and maintain code integrity throughout the development lifecycle.

For beginners embarking on version control for their software, this article serves as a mantra. If you have any questions or need further clarification, feel free to leave a comment โ€“ your feedback is welcome!

3
Subscribe to my newsletter

Read articles from Raju Gowda directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Raju Gowda
Raju Gowda

Java Dev based in India. My playground? Web services, RESTful APIs, Spring Security, and React.