Top Git Commands

Top Git Commands

ยท

3 min read

Logout- Remove your user settings

  •     # Unset global configurations
        git config --global --unset user.name
        git config --global --unset user.email
        git config --global --unset credential.helper
    
        # Clear cached credentials (if using cache helper)
        git credential-cache exit
    

Removes the file or directory from staging area without deleting it from your local filesystem

  •   git rm --cached <file or dir to be remove from staging>
    

Setting up global config for 1st time

  •         git config --global user.email "you@example.com"
            git config --global user.name "Your Name"
    

Initialize Repository

  •     git init
    

Determine current branch

  •     git branch
    

Switch Between Branches

  •     git checkout main
    

Rename Branch

  •     # Rename branch name from master to main
        git branch -m master main
    

Create a New Branch

  •     git checkout -b blog-post-title
    

Delete the Feature Branch

  •     git branch -d feature
    

Add Your Content

  •     git add <file-name>
    

Status of current state of the working directory & staging area

  •     git status
    

Commit Your Changes

  •     git commit -m "message"
    

Add URL

  •     git remote add origin <remote repo address>
    

Check Current URL

  •     git remote -v
    

Update remote repo URL if, it has changed & can also add PAT token -> login

  •     git remote set-url origin <new-url>
    

Push to Remote Repository

  •     # orgin(default)- remote repo name
        # main- remote repo branch name
        git push origin main
    
        #upstream reference
        git push -u origin main
    

Version and Commit Changes in Repository

  •     git log
    

Merge Options

  1. Cherry-Pick: Include Specific Changes from One Branch Without Merging All Changes

     git cherry-pick
    
  2. Merge: Merge Commit that Combines the Histories of the Branches

     git merge <branch-name>
    
  3. Rebase: Reapply Commits on Top of Another Base Commit, Resulting in a Linear History

     git rebase <branch-name>
    

Types of authentication to push changes to remote repo

  • SSH (Secure Shell) Authentication:

    • Uses public-private key pairs.

    • Provides secure, password-less access.

    • URL format: git@github.com:username/repository.git.

  • HTTPS with Personal Access Token (PAT):

  • OAuth (Open Authorization):

    • Token-based authentication without directly exposing credentials.

    • Often used with third-party services like GitHub Apps.

  • GPG (GNU Privacy Guard) Signing:

    • Signs commits and tags with GPG keys for authenticity verification.
  • Kerberos Authentication:

  • Credential Manager (Windows/Mac/Linux):

    • Stores and manages credentials securely using system-specific managers like Windows Credential Manager, macOS Keychain, or git-credential-libsecret for Linux.

Feel free to share and spread the knowledge! ๐ŸŒŸ๐Ÿ˜Š Enjoy Learning! ๐Ÿ˜Š

ย