sahilrajput.com

Learn git

Quick Links:

Little git tip

Update git remote

git remote set-url origin ssh_remote_link_here

Pusihg to respective branch on remote

# file: ~/.gitconfig (tested on windows)
[push]
        default = current

Remove remote branch and local branch

# Delete the remote branch (will not delete the local branch)
# Note: you don't need to prefix it with `/origin` because you already specified origin after the `push` option
git push origin --delete myBranchName

# Delete the local branch
git branch -d myBranchName

Simple way to setup ssh git-github connection with gh cli

Source - Docs - Authenticating with GitHub from Git

gh auth login

Setup your cli to work with multiple github accounts

Step 1: Generate a ssh-key value pair i.e, private and public key. Refer my own notes: Click here

Step 2: First add your public key to github account you want access to.

Step 3: Then add entry correspondingly like below to your ~/.ssh/config file -

HOST github.com
 HostName github.com
 IdentityFile "C:\Users\Array\Documents\ssh-keys\sahil-account-1-private-key"

HOST github.com-sahilrajput03
 HostName github.com
 IdentityFile "C:\Users\Array\Documents\ssh-keys\sahil-account-2-private-key"

Step 4: (Optional: May not be needed for ArchLinux) You might want to run command ssh-add ~/.ssh/oanhnn_private_key if you are doing this on ubuntu (tested no Samaksh Ubuntu). Source: Click here

Step 5: Testing ssh connections:

ssh -T git@github.com
# OUTPUT: Hi <username_here>! You've successfully authenticated, but GitHub does not provide shell access.

ssh -T git@github.com-sahilrajput03
# OUTPUT: Hi <username_here>! You've successfully authenticated, but GitHub does not provide shell access.

# For Bitbucket
ssh -T git@bitbucket.org
# OUTPUT: authenticated via ssh key.
# OUTPUT: You can use git to connect to Bitbucket. Shell access is disabled

# For GitLab
ssh -T git@gitlab.com

Step 6: In your other than default account you can use below commadn to change name and email as well:

git config user.email "superman@org2.com"
git config user.name  "Super Man"

That’s all!

double merge or round-trip merge

image

Use local time zone for git log

Source: Click here

git config --global log.date local

Some useful commands

# Get current branch name
git branch --show-current

# Get current branch and its tracking branch (upstream branch)
git branch -vv --contains

# Update tracking branch for current branch
git branch -u origin/$(git branch --show-current)

Make current user as owner of a git repository (sometimes git show weird error because the ownder of git directory is root)

This error generally happens when we copy git repositories from a portable hard disk.

To fix this you can run command: sudo chown -R array: test. Here we are marking the owner and group-owner of test directory (including all its nested files/folder using option -R).

To show lines added and lines deleted log for commits

To get lines added and lines deleted per file we can use --numstat:

git diff a73c6a44ba HEAD --numstat
# NOTE: ^^ Above command shows changes made after `a73c6a44ba` till current HEAD. (i.e, it doesn't include changes made in `a73c6a44ba` commit becoz thats how `git diff SHA1 SHA2` command works).
# NOTE: Also, git diff a73c6a44ba  (this command shows changes made after commit `a73c6a44ba` till HEAD becoz thats how `git diff SHA` command works.
# OUTPUT (first number is added lines and second number is deleted lines:
7       0       src/components/layout/main-site-wrapper/authenticated/AuthenticatedPageWrapper.tsx
11      1       src/components/ui/ReportModal.tsx
14      2       src/components/ui/post/PostDetail.tsx

Similarly to get complete info for all flies we can use --shortstat:

 git diff a73c6a44ba HEAD --shortstat
 # OUTPUT:
 14 files changed, 89 insertions(+), 40 deletions(-)

To view for a very old for single commit you can use:

git show SHA --shortstat
git show SHA --numstat

Set email and name globally

git config --global user.name "Sahil Rajput"
git config --global user.email "sahilrajput03@gmail.com"

disable pager in git output

git --no-pager log
git --no-pager show <commit_hash>

Also, the best way is simply set pager to cat, life is amazing now! (source)

# File: ~/.gitconfig
[core]
         pager = cat

You can use git config --global core.pager "cat" to set cat as pager for git log.

show all branch

git branch: Prints all branches and * indicated is the current branch.

Creating new branch

git branch test : will make test named branch.

git branch somebranch SourceBranchHere will make somebranch named branch.

switching to other branch

git checkout branchName

logs

git log

Remove a .env file from entire history of the repo

Click here

What is .gitkeep

image

this is how you include a folder with a single file .gitkeep but ignore its other contens for personal usage for each team member

image

git pull vs. git pull origin vs. git pull origin main

image

View the commit diff of merged commit?

That can work actually.

image

♥💕❤😘 You can literally move your .git folder any where

Yes, it sounds quite unbelievable but its quite magical how it does work actually. Amazing Linus Torwald.

What is .gitkeep file

TLDR: (its just a simple conventional hack by people to make a empty folder be indexable)

Docs: .gitignore @ SCM

Stackoverflow Answer: Click here

image

image

Addging * to a nested folder’s .gitignore ignore all files including the .gitignore file as well

image

Adding * to root .gitignore file ignore all entities

Thats all.

Commited node_modules to your repository along with the some new and modified files accidentally ?

The solution is recommended when you can manage to put all the commits after the nodemodules added to the repository to be squashed to a single commit because its a very _naive solution of my own to remove node_modules from the git history.

Remove node_modules from the most recent commit only via --amend method:

# You can literally copy paste below commands to fix the shit.
git rm --cached -r .                          # Reset the tracking area.
echo node_modules >> .gitignore
git add . && git commit --amend --no-edit      # Amend last commit as it is(but with node_modules ``git ignored``)!

Another way if are in situation where lots of commits are made since you added node_modules to the repository (i.e., BLUNDER HELL ehh..) via:

git branch temp                   # Make a backup branch of current branch's status.
git reset --hard HEAD             # Get to the desired/last commit where you didn't have node_modules.
git merge --no-ff temp            # Merge without making a commit.
git rm --cached -r .              # Now remove everything(_node_modules_) coz ```git merge --no-ff``` re-added everything to the the staging area.
echo node_modules >> .gitignore
git add . && git commit -m 'My new commit without node_modules.'