Big Book of Git

git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Amongst version-control systems, git is by far the most used. Almost all open-source projects are developed using Git and GitHub. In enterprise there is more various in version control system usage due to security preferences, etc. This is a resource to keep track of less intuitive git operations I use extremely frequently as a developer.

NOTE

My preferred interface for working with git repositories in the command line AND within my text editor is lazygit - a simple terminal UI for git commands.

Branch Management

Renaming local branches and synchronizing those changes to a remote repository: Source: stackoverflow thread

  1. Rename your local branch:

    • If you are on the branch you want to rename (new-name and old-name should be replaced with the strings of the names you intend):

      git branch -m new-name
    • If you are on a different branch:

      git branch -m old-name new-name
  2. Delete the old-name remote branch and push the new-name local branch:

    git push origin :old-name new-name
  3. Reset the upstream branch for the new-name local branch:

    • Switch to the branch and then:

      git push origin -u new-name

Staging Area / Pre-commit management

git reset is an API to reset the current HEAD of the branch to a specified state.

Oftentimes I have unstaged changes in my working directory which I want to nuke because they are no longer relevant to the project. That’s why it’s a good habit to develop in feature branches and pull changes from the master branch every day, keeping your branch fresh. It’s easy to add too much content to a single PR. Use discretion.

To revert to a previous commit, use

  • git reset --hard HEAD

links:

Generally Helpful Utilities

git diff is a great API to

Show changes between commits, commit and working tree, etc

The etc part is where it really shines! I use it frequently as a Lazygit custom command to do change management on my work. Here’s an article by GitLab reviewing Git version control best practices for developers. GitLab is “the most comprehensive DevSecOps platform” started in 2011. GitLab is slightly less popular than GitHub but seems to be more popular in enterprise. I plan to do a deeper article sometime about different git-based cloud platform tooling options.

  • GH enterprise: codespaces
  • some enterprises “mirror” repositories between gitlab and GH

links: