Git-Cheatsheet


Git


Git-Cheatsheet


GIT LOG

Placeholder that expand to information extracted from the commit :



%H - commit hash

%h - abbreviated commit hash

%T - tree hash

%t - abbreviated tree hash

%P - parent hashes

%p - abbreviated parent hashes

%an - author name

%aN - author name

%ae - author email

%aE - author email


CHECKOUT


UNDO


Working with git Server


Adding SSH key to your github account

Configuring Git for a Second GitHub Account

If you have multiple GitHub accounts and want to use different accounts for specific repositories, you can configure Git to switch between them. Here’s how to set up Git to use a second GitHub account for a particular repository:

  1. Configure SSH Key Pair for the Second GitHub Account:

    First, ensure you have an SSH key pair set up for the second GitHub account you want to use:

    • Generate a new SSH key pair for the second GitHub account:

      ssh-keygen -t ed25519 -f ~/.ssh/id_ed25519_second_account -C "your_email@example.com"
      
    • Add the SSH key to your SSH agent:

      eval "$(ssh-agent -s)"
      ssh-add ~/.ssh/id_ed25519_second_account
      
    • Copy the public key (id_ed25519_second_account.pub) to your second GitHub account’s SSH settings in the GitHub account settings.

  2. Edit Your SSH Configuration:

    To specify which SSH key to use for which GitHub account, create or edit your ~/.ssh/config file. If this file doesn’t exist, create it. Add the following configuration:

    # Default GitHub account (first account)
    Host github.com
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_ed25519
    
    # Second GitHub account
    Host github-second
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_ed25519_second_account
    

Replace github-second with a label you choose for the second GitHub account.

  1. Update Git Configuration for the Specific Repository:

Navigate to the local repository where you want to use the second GitHub account. In that repository’s directory, configure Git to use the second GitHub account by updating the origin remote URL:

First, check the current URL of the remote:

git remote -v

Change the URL of the remote to use the second GitHub account:

git remote set-url origin git@github-second:username/repo.git

Replace username/repo.git with the repository details you want to work with using the second GitHub account.

Now, when you interact with the specific repository, Git will use the SSH key and configuration for the second GitHub account.

Repeat these steps for other repositories where you want to use the second GitHub account. This way, you can have multiple GitHub accounts configured on your local machine and use them as needed.

Setting aliases in Git


List of some aliases I use myself : edit using git config --global --edit

[alias]
        s = status
        a = add .
        b = checkout -b
        br = branch
        del = delete -D
        p = push
        edit = config --global --edit
        m = branch -M main
        co = checkout
        ci = commit -m
        cia = commit -am
        lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative


BRANCH


Types of branching workflow = 1.Long Running Branches 2.Short Running Branches


GIT STASH



TAG



SHOW



RENAMING GIT BRANCH

Undoing mistakes with git using the command line

Extra Tools

  1. git archive (http://www.kernel.org/pub/software/scm/git/docs/git-archive.html) - Creates a tar or zip file of the contents of a single tree from your repository. Easiest way to export a snapshot of content from your repository.

  2. git gc (http://www.kernel.org/pub/software/scm/git/docs/git-gc.html) - Garbage collector for your repository. Packs all your loose objects for space and speed efficiency and optionally removes unreachable objects as well. Should be run occasionally on each of your repos.

  3. git fsck (http://www.kernel.org/pub/software/scm/git/docs/git-fsck.html) - Does an integrity check of the Git “filesystem”, identifying dangling pointers and corrupted objects.

  4. git prune (http://www.kernel.org/pub/software/scm/git/docs/git-prune.html) - Removes objects that are no longer pointed to by any object in any reachable branch.

  5. git daemon (http://www.kernel.org/pub/software/scm/git/docs/git-daemon.html) - Runs a simple, unauthenticated wrapper on the git-upload-pack program, used toprovide efficient, anonymous and unencrypted fetch access to a Git repository.


Resources