Git for Beginners by Me
Set git user info (assuming that you are the only user of this System)
git config --global user.name <name>
git config --global user.email <email>User info for a certain Repository only
git config user.name <name>
git config user.email <email>Initialize git before using git commands
git initAdd files on staging area to be committed/Save
git add <file_name>
git add . # stages all the files togetherSee current git status
git statusUnstages file(s) from staging area
git restore --staged <file_name> OR .Untrack a file/directory permanently from git
git rm --cached <file_name>Untrack a file/directory temporarily from git
git update-index --assume-unchanged path/to/fileTrack files which were set to Untrack temporarily in git
git update-index --no-assume-unchanged path/to/filesee the list of files set to Untrack temporarily in git
git ls-files -v | grep "^h "The same list on Windows PowerShell
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.
git restore <file_name> OR .Create a Branch
git branch <name_of_branch>Visit/Enter a Branch
git checkout <name_of_branch>Visit a commit from logs
git checkout <SHA256_key_of_the_commit>See logs of the branch
git logTo checkout the branch when creating it, use
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.
git push -u origin <branch> # -u is short for --set-upstreamNote:- You can only create a single PR from a same branch.
Merge branch
git merge <name_of_branch>Delete a Branch. It only works if the branch is already merged with the master branch.
git branch -d <name_of_branch>Force delete a Branch. It gives an error if you are on that branch while deleting it.
git branch -D <name_of_branch>See all the files on staged area.
git diff --name-only --cachedSee git index
git ls-filessee only modified files
git ls-files -mStage only modified files
git add -uCommit only modified files
git commit -aSee status of only tracking files in git
git status -unoIn case of multiple SSH Github account keys check which one is being used by default
ssh -T git@github.com
>>> Hi, HarshalRathore
ssh -T git@college.github.com
>>> Hi, HarshalRathore007For using multiple SSH keys associated with different github account
Create a ~/.ssh/config file
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-userSo 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