Git Command Summary

Getting Started

  1. Initialize a Repository:


    git init

    Creates a new Git repository in your project directory.

  2. Clone a Repository:


    git clone <repository_url>

    Downloads a repository from a remote source (e.g., GitHub).

  3. Set Up Git Configuration:

    • Set your username:


      git config --global user.name "Your Name"
    • Set your email address:


      git config --global user.email "your.email@example.com"
    • Check your configuration:


      git config --list

      Displays all configured settings.

    • Set your preferred editor (e.g., VS Code):


      git config --global core.editor "code --wait"

    Note: The --global flag applies settings to all repositories; omit it for repository-specific settings.


Basic Commands

  1. Check the Status:


    git status

    Shows the current status of your files (e.g., staged, modified, untracked).

  2. Add Files to Staging:


    git add <file_name> git add .

    Adds files to the staging area (. stages all files).

  3. Commit Changes:


    git commit -m "Commit message"

    Saves the staged changes with a message describing them.


Working with Changes

  1. View Changes:


    git diff

    Displays changes between the working directory and the staging area.

  2. View Commit History:


    git log

    Shows the commit history with details.

  3. Undo Changes:

    • Unstage a file:

      git reset <file_name>
    • Discard changes in a file:

      git checkout -- <file_name>
  4. Revert a Commit:


    git revert <commit_hash>

    Creates a new commit that undoes the changes made by a specific commit (safe for collaborative workflows).

  5. View Reference Logs:


    git reflog

    Shows a log of all the actions (e.g., commits, checkouts) performed in your repository. Useful for recovering lost commits.


Branching and Merging

  1. Create a New Branch:


    git branch <branch_name>

    Creates a new branch.

  2. Switch to a Branch:


    git checkout <branch_name>

    Moves to the specified branch.

  3. Merge Branches:


    git merge <branch_name>

    Merges another branch into the current branch.

  4. Delete a Branch:


    git branch -d <branch_name>

Working with Remotes

  1. View Remote Repositories:


    git remote -v

    Lists remote repositories.

  2. Push Changes to Remote:


    git push origin <branch_name>

    Sends changes to a remote repository.

  3. Pull Changes from Remote:


    git pull origin <branch_name>

    Fetches and merges changes from a remote repository.

  4. Fetch Remote Changes:


    git fetch

    Retrieves changes from a remote repository but does not merge them.


Advanced Essentials

  1. Stash Changes:


    git stash

    Temporarily saves uncommitted changes.

  2. Apply Stashed Changes:


    git stash apply
  3. Tagging a Commit:


    git tag <tag_name>

    Adds a tag to a specific commit.

  4. Recover Lost Actions:


    git reflog

    Tracks all changes (even detached HEAD or commits accidentally dropped) to recover lost work.


Bonus: Setting Up Aliases

  1. Create Shortcuts for Commands:

    git config --global alias.st status git config --global alias.co checkout git config --global alias.br branch git config --global alias.ci commit
    Use these shortcuts to speed up common commands (e.g., git st instead of git status).

Comments

Popular posts from this blog

IT Workshop GXESL208 KTU BTech 2024 Scheme - Dr Binu V P

Familiarization of basic Linux Commands- ls, mkdir, rmdir , rm, cat, cp, mv , chmod