Git It Done - A Cheat Sheet for Version Control Ninjas

Git It Done - A Cheat Sheet for Version Control Ninjas

GIT is a Version Control System (VCS) that helps developers manage changes to their codebase. Here are the key points:

  1. Version Control

    • Git tracks changes to files over time.

    • It allows you to revert to previous versions, collaborate with others, and manage code history.

  2. Distributed System

    • Each developer has a complete copy of the repository.

    • Changes can be synchronised between local and remote repositories.

  3. Basic Concepts

    • Repository: A project’s folder managed by Git.

    • Commit: A snapshot of changes (code, files) with a unique hash.

    • Branch: Independent line of development.

    • Merge: Combining changes from one branch into another.

    • Pull Request (PR): Proposing changes to a repository.

  4. Branching Strategies:

    • Feature Branches: Isolate new features or bug fixes.

    • Main Branch (usually ‘master’ or ‘main’): Represents stable code.

    • Release Branches: Prepare for production releases.

  5. Collaboration:

    • Git enables collaboration among developers.

    • Platforms like GitHub, GitLab, and Bitbucket host Git repositories.

  6. Common Commands : Let’s explore some of the commonly used Git commands along with their explanations and examples. These commands are essential for managing your Git repositories effectively.

    1. git init
  • Initialises a new Git repository in the current directory.

  • Creates a hidden .git subfolder to store version control data.

      git init
    
  1. git clone
  • Creates a local copy of a remote Git repository.

  • Useful for starting work on an existing project.

      git clone <repository_url>
    
  1. git add
  • Stages changes for commit.

  • Adds modified files to the staging area.

      git add myfile.txt
    
  1. git commit
  • Records changes in the repository.

  • Creates a snapshot of the staged changes.

      git commit -m "Add new feature"
    
  1. git status
  • Shows the current status of the working directory.

  • Displays untracked, modified, and staged files.

      git status
    
  1. git push
  • Uploads local commits to a remote repository.

  • Updates the remote branch with your changes.

      git push origin main
    
  1. git pull
  • Fetches changes from a remote repository.

  • Updates your local branch with the latest changes.

      git pull origin main
    
  1. git branch
  • Lists existing branches or creates a new branch.

  • Useful for managing different features or bug fixes.

      git branch feature-branch
    
  1. git merge
  • Combines changes from one branch into another.

  • Merges feature branches into the main branch.

      git merge feature-branch
    
  1. git stash
  • Temporarily saves changes without committing.

  • Useful for switching branches without losing work.

      git stash save "Work in progress"
    
  1. Advanced Commands : Let’s delve deeper into advanced Git commands and explore additional details about Git. Understanding these commands will enhance your proficiency in version control.

    1. git log
  • Displays a chronological list of commits in the repository.

  • Useful for tracking changes, understanding commit history, and identifying authors.

      git log
    
  1. git diff
  • Shows differences between working directory, staging area, and the last commit.

  • Helps identify changes before committing.

      git diff myfile.txt
    
  1. git reset
  • Unstages the changes from the staging area.

  • Can be used to uncommit changes (with options like --soft, --mixed, or --hard).

  • Example for unstaging the changes to a file:

      git reset myfile.txt
    
  • Example for uncommit the changes:

      git reset --soft HEAD^
    
  1. git rebase
  • Reapplies commits from one branch onto another.

  • Useful for maintaining a linear commit history.

  • Example for rebase feature branch onto main branch:

      git checkout feature
      git rebase main
    
  1. git cherry-pick
  • Selectively applies specific commits from one branch to another.

  • Useful for incorporating specific changes without merging entire branches.

      git cherry-pick <commit_hash>
    
  1. git tag
  • Creates a named reference to a specific commit (e.g., for releases or milestones).

  • Useful for marking important points in history.

      git tag v1.0.0
    
  1. git bisect
  • Helps find the commit that introduced a bug.

  • Binary search through commit history.

      git bisect start
      git bisect bad <bad_commit>
      git bisect good <good_commit>
    
  1. git submodule
  • Manages external dependencies as submodules within your repository.

  • Useful for including external libraries or projects.

      git submodule add <repository_url> path/to/submodule
    
  1. git reflog
  • Shows a log of all references (branches, HEAD, stashes) even after they’ve been deleted.

  • Useful for recovering lost commits or branches.

      git reflog
    
  1. git clean
  • Removes untracked files and directories from the working directory.

  • Useful for cleaning up unwanted files.

  • Example (dry run):

      git clean -n
    
  • Example (force removal):

      git clean -f
    

    Remember, Git is a powerful tool, and mastering these commands will make you a more efficient developer. Keep exploring and experimenting!

    To Learn More about GIT - GitHub Docs