Quick Links:
git remote rename <old_name> <new_name>
Source: Click here
Note: Make sure you restart vscode after you use below commands.
git config --global commit.template ~/.gitmessage
echo Update. > ~/.gitmessage
If you would have specified
git push origin master:my_work
then you would have pushed your local master
to origin/my_work
. If you don’t use the :my_work
part, then the destination defaults to the same branch as given as source.
git remote set-url origin ssh_remote_link_here
# file: ~/.gitconfig (tested on windows)
[push]
default = current
# 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
gh
cliSource - Docs - Authenticating with GitHub from Git
gh auth login
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
Source: Click here
git config --global log.date local
# 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)
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 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
git config --global user.name "Sahil Rajput"
git config --global user.email "sahilrajput03@gmail.com"
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.
git branch
: Prints all branches and * indicated is the current branch.
git branch test
: will make test
named branch.
git branch somebranch SourceBranchHere
will make somebranch
named branch.
git checkout branchName
git log
git pull
vs. git pull origin
vs. git pull origin main
SD-499
which was checked out from main.git pull
or git pull origin
, this will only fetch and merge all remote branches to their respective local branches which set set t to tracked.main
branch into your current branch either by doing git merge main
or you can do it by git pull origin main
as both will do same thing. Why? Ans. Because git pull origin main
will pull update local main branch first and second it will merge main
branch to your currently checkout branch
. BECAUSE git pull
= git fetch
+ git merge branch
. Source: Atlassian, Git-scmThat can work actually.
.git
folder any whereYes, it sounds quite unbelievable but its quite magical how it does work actually. Amazing Linus Torwald.
.gitkeep
fileTLDR: (its just a simple conventional hack by people to make a empty folder be indexable)
Docs: .gitignore @ SCM
Stackoverflow Answer: Click here
*
to a nested folder’s .gitignore
ignore all files including the .gitignore
file as well*
to root .gitignore
file ignore all entitiesThats all.
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.'