← all writing

Git for Beginners by Me

· 3 min read · git / version-control / github

Set git user info (assuming that you are the only user of this System)

sh
git config --global user.name <name>
git config --global user.email <email>

User info for a certain Repository only

sh
git config user.name <name>
git config user.email <email>

Initialize git before using git commands

sh
git init

Add files on staging area to be committed/Save

sh
git add <file_name>
git add .  # stages all the files together

See current git status

sh
git status

Unstages file(s) from staging area

sh
git restore --staged <file_name> OR .

Untrack a file/directory permanently from git

sh
git rm --cached <file_name>

Untrack a file/directory temporarily from git

sh
git update-index --assume-unchanged path/to/file

Track files which were set to Untrack temporarily in git

sh
git update-index --no-assume-unchanged path/to/file

see the list of files set to Untrack temporarily in git

sh
git ls-files -v | grep "^h "

The same list on Windows PowerShell

sh
git ls-files -v |? {$_ -cmatch '^h '}

Restore deleted file(s) or reverse the Change in file to last commit. Files to be restored should have been committed.

sh
git restore <file_name> OR .

Create a Branch

sh
git branch <name_of_branch>

Visit/Enter a Branch

sh
git checkout <name_of_branch>

Visit a commit from logs

sh
git checkout <SHA256_key_of_the_commit>

See logs of the branch

sh
git log

To checkout the branch when creating it, use

sh
git checkout -b branchname <sha1-of-commit or HEAD~3>

Note:- if you created a branch and committed some changes you can’t directly push it to remote, first you have to publish it and also set git to auto track it so you can use git push git pull directly.

sh
git push -u origin <branch>  # -u is short for --set-upstream

Note:- You can only create a single PR from a same branch.

Merge branch

sh
git merge <name_of_branch>

Delete a Branch. It only works if the branch is already merged with the master branch.

sh
git branch -d <name_of_branch>

Force delete a Branch. It gives an error if you are on that branch while deleting it.

sh
git branch -D <name_of_branch>

See all the files on staged area.

sh
git diff --name-only --cached

See git index

sh
git ls-files

see only modified files

sh
git ls-files -m

Stage only modified files

sh
git add -u

Commit only modified files

sh
git commit -a

See status of only tracking files in git

sh
git status -uno

In case of multiple SSH Github account keys check which one is being used by default

sh
ssh -T git@github.com
>>> Hi, HarshalRathore
ssh -T git@college.github.com
>>> Hi, HarshalRathore007

For using multiple SSH keys associated with different github account

Create a ~/.ssh/config file

ssh config
Host college.github.com
  HostName github.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_ed25519_harshal24ai017

Host github.com
  HostName github.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_ed25519_harshalrathore2014

Host ec2-13-234-110-107.ap-south-1.compute.amazonaws.com
  HostName ec2-13-234-110-107.ap-south-1.compute.amazonaws.com
  IdentityFile ~/harshal/newkeypair.pem
  User ec2-user

So when you want to Clone college id repo replace git@github.com…. to git@college.github.com… and everything will work like same. pushing will not give error