Git &GitHub - The commands and features you must know

Git &GitHub - The commands and features you must know

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

  2. 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.

  3. 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.

  4. 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.

  5. 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

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

  • git add [file-name]
    Stages the specified files. multiple file paths can be specified

  • 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.

  • 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).

  • git checkout [existing-branch-name]
    Switches to the specified branch from the current branch.

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

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

  • 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.

  • git branch
    Prints all the local branches present.

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

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

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

  • git push
    Pushes the code to the global repository with the same name as the current branch of the local 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.

  • git push origin [branch-name]
    Pushes the code to the branch with the specified name in the global repository.

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

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

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

💡
When 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!