Git and GitHub Notes
Notes on git and github
Bash Commands
-
pwd-------------------Present Working Directory path -
ls-------------------- To view directories and files in a folder -
ls -R----------------- To view subdirectories of directories. -
ls -t -
ls -l------------------To view permissions, last modified date, size in bytes of particular folder. -
ls -lt -
ls -la-----------------To view all items including hidden items. -
ls -lRa -
ls -lr-----------------shows in reverse order. -
ls -s------------------To view size. -
ls *.js----------------To view all the js files in that folder. -
ls Zoo*----------------To view all the files with "Zoo" in its name. -
ls ..------------------list all directories and folder. -
cd---------------------------Go to -
cd ..------------------------Go to previously directory. -
cd ../../--------------------Go back twice. -
touch------------------------To create a file. {touch a.js} -
cat--------------------------To view what is inside in a file. {cat a.js} -
cat > a.txt------------------To write something in a file. ctrl + D to save and exit. ctrl + C to exit. -
cat >> a.txt -
mkdir------------------------------create a directory of name test. {mkdir test} -
mkdir test && cd test--------------create a new directory and go inside that directory. -
mkdir -p---------------------------To create directory inside directory. {mkdir -p frontend/scripts} -
mv---------------------------------To move files. {mv script.js runtime_script.js} -
mv filepath/newname----------------To rename a file. -
cp---------------------------------To copy files {cp filepath new filepath} -
cp -r------------------------------To copy a directory. -
rm filename------------------------To delete a file. -
rm -r folderpath-------------------To delete a folder. -
chmod ugo-rwx---------------------------To add permission to a file. -
chmod -R ugo-rwx------------------------To add permission to a folder. -
chmod u+x filename -
chmod g+wx filename -
chmod u-x filename
1-x, 2-w, 4-r
-
chmod 664folder name -
echo 'Hello World'---------------------------To display a certain message. -
head filename-------------------------------View us the first 10 rows of a file. -
tail filename-------------------------------View us the last 10 rows of a file. -
head -20 filename---------------------------View the first 20 rows of a file. same goes with tail. -
tail -n +25 filename | head -n +5-----------To view custom rows. -
wc filename---------------------------------To view linecount, wordcount, charactercount of a file. -
grep "one" filename------------------where "one" has been used in the file. -
grep "one" filename | wc -l----------how many times "one" has been used in the file. -
grep -c "one" filename---------------how many times "one" has been used in the file. -
grep -h "one" filename---------------where "one" has been used in the file. (case sensitive) -
grep -hi "one" filename--------------where "one" has been used in the file. (not case sensitive) -
grep -hir "one" directoryname--------where "one" has been used in the folder. -
grep -hin "one" filename-------------where "one" has been used in the file inc line numbers. (not case sensitive) -
grep -hinw "one" filename------------where "one" has been used inside a word also individually. {colone, one, One} (case sensitive) -
grep -o "one" filename---------------only gives us the matched part. -
grep -w "one" filename---------------where "one" has been used in the file. -
history------------------------------to view all the command that i've used. -
bash filename -
grep "ERROR" filename----------------will view all the error messages in that file. -
grep -v "INFO" filename -
grep -A 5 ERROR filename-------------to view rows after the occurance of ERROR text in a file -
grep -B 5 ERROR filename-------------to view rows before the occurance of ERROR text in a file -
grep -C 5 ERROR filename-------------to view rows before and after the occurance of ERROR text in a file. -
sed -n '/ERROR/ p' filename------------------to print lines with ERROR text. -
sed 's/ERROR/CRITICAL' filename--------------Replace ERROR with CRITICAL in the file. -
sed -ibackup 's/ERROR/CRITICAL/' filename----Create a backup of the file. -
sed '3 s/CRITICAL/VERYCRITICAL/' filename----Replace CRITICAL with VERYCRITICAL in line number 3. -
sed '3,5 s/ERROR/CRITICAL/' filename---------Replace CRITICAL with VERYCRITICAL in line number 3 to line number 5. -
sed -n '3,/ERROR/ p' filename -
awk '/ERROR/{print $0}' filename-------------------------to print lines with ERROR text. -
awk '{gsub(/ERROR/, "CRITICAL")}{print}' filename---------Replace ERROR with CRITICAL in the file. -
awk 'BEGIN {print "LOG SUMMARY\n--------------"} {print} END {print "--------------\nEND OF LOG SUMMARY"}' filename ---------add text in the beginning and ending of a file. -
awk '{print $1, $2}' filename----------------------------Print 1st and the 2nd column of the data (file). -
awk -F "," '{print $1, $2}' filename -
awk '{count[$2]++} END {print count["ERROR]}' filename---Count the occurance of ERROR in second column of the file. -
awk '{ if ($1 > 1598863888 ) {print $0} }' log.txt-------view the rows after 1598863888 in first column.
Git
git statusto check statusgit initto initialized git
Commit


git add {FILE_NAME}or forall git add .to tracking or staging areagit commit -m “{MESSAGE}”to make file in commit stagegit commit -am “{MESSAGE}”shortcut to add and commit.git logto logs all commitsgit log —onelinegives logs in one line
Atomic Commits
- Keep commits centric to one feature, one component or on fix
- Present or past commit msg depends {Present tense, imperative} give order to code base don’t care
git config —global user.name “Bashar Khan”anduser.email “basharkhan7776@gmail.com”- .gitignore generator to generate that project
Branch

git branchshow the current branch namegit branch -M mainto rename to main (mainly in open sourced)git branch {BRANCH_NAME}create new branchgit checkout {BRANCH_NAME}orgit switch {BRNCH_NAME}switch branchgit checkout -b {BRANCH_NAME}orgit switch -c {BRNCH_NAME}create and switch branch

Merging
git checkout mastergit merge {BRACNH_NAME}
⚡ After merging it may give Conflict , which may be resolve in vs code
git diff --stagedto see the difference after stage filegit diff {BRANCH1} {BRANCH2}also gives the diff b/w branchesgit diff {LOG_ID1} {LOG_ID1}also gives the diff b/w log id's

Stash
git stashchange branch without commit. (like temp storage)git stash popto bring back prev code.

More Command
git checkout {HASH}go to that commit files.git switch mainre-attach to Head.git checkout HEAD~2look at 2 commit priorgit restore {FILE_NAME}get back to last commit version
Rebase
🚫Never run this command in main or master branch
git rebase masterjoin branch to master or other branch and clean up prev history

⚡ Never rebase commits that have shared.
→ Push to Github → Never rebase
GitHub
→ Github is the service to host things online.
-
git clone {URL}to clone any repo. -
git remote -vlist of remote repo in git. -
git remote {NAME} {URL}add remote file to locally. -
git remote origin https://www.github.ksjnfannsdf.gitmainly use in github. -
git remote add origin https://www.github.ksjnfannsdf.gitmainly use in github. -
git remote rename {OLD_NAME} {NEW_NAME}to rename remote file. -
git remote remove {NAME}to remove remote file. -
git push {REMOTE_NAME} {BRANCH_NAME}push file to remote from branch. -
git push origin mainpush only one time -
git push -u origin mainorgit push --set-upstream origin mainsetup an upstream that allow u to run future git push command and push code directly to github.

Open Sourced Contributions

- First fork in repository so admin make eye on you.
- Clone to your machine.
- Make another Branch in git, don't interrupt main
- Done your work and the Push that branch into remote
- Back to GitHub and u well see
Create Pull Requestin site - Add Title and Description (markdown syntax)
- Can PR to main or can other branch
Problems in connecting in github with WSl :
→ In PR it ask username and password.
→ Make classic token from GitHub profile Setting > Developers Setting > Token(Classic).
→ Make one token for auth save it for later.
→ In WSL after PR, enter username and in pass past saved token.
Reference
https://www.youtube.com/watch?v=zTjRZNkhiEU&list=WL
https://git-scm.com/doc
https://docs.github.com/en