Quick Links:
- Github Docs Repository: https://github.com/github/docs, package.json
- Older notes: https://sahilrajput03.github.io/Git_and_GitHub.html
Not possible, please do it by first cheking out main branch!
git add -f path/to/file/or/folder1/3 Create new branch ruleset:
2/3 Give name to the ruleset and add desired bypass roles i.e., people to who the rules will not apply:
3/3 - Rules:
In Git:
headbranch: The current branch you're working on.basebranch: The branch you're merging into or comparing against, typically the target branch.
It depends on your goal:
- Squash: Best for keeping a clean history by merging multiple commits into one before merging to the main branch.
- Rebase: Best for keeping a linear history by applying changes on top of the latest branch state.
If you want a single, clean commit, squash is better. If you want to integrate changes smoothly while preserving individual commits, rebase is better.
Give me an example of git command to work with rebase.
Here’s an example of how to use git rebase:
Rebasing your branch onto main
git checkout feature-branch
git fetch origin
git rebase origin/main
# Interactive rebase - This lets you edit, squash, or reorder commits during the rebase process.
# git rebase -i origin/main
# You would need to force push your feature branch if you have pushed to to feature branch before you ran `rebase` command because you have overwritten the commit history
# git push origin feature-branch --force
# Safer Alternative: Instead of `--force`, a safer option is `--force-with-lease`:
git push origin feature-branch --force-with-lease
# This ensures you don’t accidentally overwrite someone else’s changes.
# It only forces the push if the remote hasn’t changed since your last fetch..ignored/*
!.ignored/.keepgit 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_workthen you would have pushed your local
mastertoorigin/my_work. If you don't use the:my_workpart, 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 myBranchNameSource - Docs - Authenticating with GitHub from Git
gh auth loginStep 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.comStep 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!
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)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 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.tsxSimilarly 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 --numstatgit 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](Source: Click here))
# File: ~/.gitconfig
[core]
pager = catYou 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
this is how you include a folder with a single file .gitkeep but ignore its other contens for personal usage for each team member
- You're working on a branch called
SD-499which was checked out from main. - Now main branch on the remote repository is updated.
- Now you do
git pullorgit pull origin, this will only fetch and merge all remote branches to their respective local branches which set set t to tracked. - SO, now you need to merge
mainbranch into your current branch either by doinggit merge mainor you can do it bygit pull origin mainas both will do same thing. Why? Ans. Becausegit pull origin mainwill pull update local main branch first and second it will mergemainbranch to yourcurrently checkout branch. BECAUSEgit pull=git fetch+git merge branch. Source: Atlassian, Git-scm
That can work actually.
Yes, it sounds quite unbelievable but its quite magical how it does work actually. Amazing Linus Torwald.
TLDR: (its just a simple conventional hack by people to make a empty folder be indexable)
Docs: .gitignore @ SCM
Stackoverflow Answer: Click here
Thats 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-commit --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.'






