BPathan.
    Back to Notes

    Git and GitHub Notes

    Notes on git and github

    28-04-26

    Bash Commands

    1. pwd -------------------Present Working Directory path

    2. ls -------------------- To view directories and files in a folder

    3. ls -R ----------------- To view subdirectories of directories.

    4. ls -t

    5. ls -l ------------------To view permissions, last modified date, size in bytes of particular folder.

    6. ls -lt

    7. ls -la -----------------To view all items including hidden items.

    8. ls -lRa

    9. ls -lr -----------------shows in reverse order.

    10. ls -s ------------------To view size.

    11. ls *.js ----------------To view all the js files in that folder.

    12. ls Zoo* ----------------To view all the files with "Zoo" in its name.

    13. ls .. ------------------list all directories and folder.

    14. cd ---------------------------Go to

    15. cd .. ------------------------Go to previously directory.

    16. cd ../../ --------------------Go back twice.

    17. touch ------------------------To create a file. {touch a.js}

    18. cat --------------------------To view what is inside in a file. {cat a.js}

    19. cat > a.txt ------------------To write something in a file. ctrl + D to save and exit. ctrl + C to exit.

    20. cat >> a.txt

    21. mkdir ------------------------------create a directory of name test. {mkdir test}

    22. mkdir test && cd test --------------create a new directory and go inside that directory.

    23. mkdir -p ---------------------------To create directory inside directory. {mkdir -p frontend/scripts}

    24. mv ---------------------------------To move files. {mv script.js runtime_script.js}

    25. mv filepath/newname ----------------To rename a file.

    26. cp ---------------------------------To copy files {cp filepath new filepath}

    27. cp -r ------------------------------To copy a directory.

    28. rm filename ------------------------To delete a file.

    29. rm -r folderpath -------------------To delete a folder.

    30. chmod ugo-rwx ---------------------------To add permission to a file.

    31. chmod -R ugo-rwx ------------------------To add permission to a folder.

    32. chmod u+x filename

    33. chmod g+wx filename

    34. chmod u-x filename

    1-x, 2-w, 4-r

    1. chmod 664 folder name

    2. echo 'Hello World'---------------------------To display a certain message.

    3. head filename -------------------------------View us the first 10 rows of a file.

    4. tail filename -------------------------------View us the last 10 rows of a file.

    5. head -20 filename ---------------------------View the first 20 rows of a file. same goes with tail.

    6. tail -n +25 filename | head -n +5 -----------To view custom rows.

    7. wc filename ---------------------------------To view linecount, wordcount, charactercount of a file.

    8. grep "one" filename ------------------where "one" has been used in the file.

    9. grep "one" filename | wc -l ----------how many times "one" has been used in the file.

    10. grep -c "one" filename ---------------how many times "one" has been used in the file.

    11. grep -h "one" filename ---------------where "one" has been used in the file. (case sensitive)

    12. grep -hi "one" filename --------------where "one" has been used in the file. (not case sensitive)

    13. grep -hir "one" directoryname --------where "one" has been used in the folder.

    14. grep -hin "one" filename -------------where "one" has been used in the file inc line numbers. (not case sensitive)

    15. grep -hinw "one" filename ------------where "one" has been used inside a word also individually. {colone, one, One} (case sensitive)

    16. grep -o "one" filename ---------------only gives us the matched part.

    17. grep -w "one" filename ---------------where "one" has been used in the file.

    18. history ------------------------------to view all the command that i've used.

    19. bash filename

    20. grep "ERROR" filename ----------------will view all the error messages in that file.

    21. grep -v "INFO" filename

    22. grep -A 5 ERROR filename -------------to view rows after the occurance of ERROR text in a file

    23. grep -B 5 ERROR filename -------------to view rows before the occurance of ERROR text in a file

    24. grep -C 5 ERROR filename -------------to view rows before and after the occurance of ERROR text in a file.

    25. sed -n '/ERROR/ p' filename ------------------to print lines with ERROR text.

    26. sed 's/ERROR/CRITICAL' filename --------------Replace ERROR with CRITICAL in the file.

    27. sed -ibackup 's/ERROR/CRITICAL/' filename ----Create a backup of the file.

    28. sed '3 s/CRITICAL/VERYCRITICAL/' filename ----Replace CRITICAL with VERYCRITICAL in line number 3.

    29. sed '3,5 s/ERROR/CRITICAL/' filename ---------Replace CRITICAL with VERYCRITICAL in line number 3 to line number 5.

    30. sed -n '3,/ERROR/ p' filename

    31. awk '/ERROR/{print $0}' filename -------------------------to print lines with ERROR text.

    32. awk '{gsub(/ERROR/, "CRITICAL")}{print}' filename---------Replace ERROR with CRITICAL in the file.

    33. awk 'BEGIN {print "LOG SUMMARY\n--------------"} {print} END {print "--------------\nEND OF LOG SUMMARY"}' filename ---------add text in the beginning and ending of a file.

    34. awk '{print $1, $2}' filename ----------------------------Print 1st and the 2nd column of the data (file).

    35. awk -F "," '{print $1, $2}' filename

    36. awk '{count[$2]++} END {print count["ERROR]}' filename ---Count the occurance of ERROR in second column of the file.

    37. awk '{ if ($1 > 1598863888 ) {print $0} }' log.txt -------view the rows after 1598863888 in first column.

    Git

    • git status to check status
    • git init to initialized git

    Commit

    Screenshot 2024-11-21 004502.png

    Screenshot 2024-11-21 004512.png

    • git add {FILE_NAME} or for all git add . to tracking or staging area
    • git commit -m “{MESSAGE}” to make file in commit stage
    • git commit -am “{MESSAGE}” shortcut to add and commit.
    • git log to logs all commits
    • git log —oneline gives 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” and user.email “basharkhan7776@gmail.com”
    • .gitignore generator to generate that project

    Branch

    image.png

    • git branch show the current branch name
    • git branch -M main to rename to main (mainly in open sourced)
    • git branch {BRANCH_NAME} create new branch
    • git checkout {BRANCH_NAME} or git switch {BRNCH_NAME} switch branch
    • git checkout -b {BRANCH_NAME} or git switch -c {BRNCH_NAME} create and switch branch

    image.png


    Merging

    1. git checkout master
    2. git merge {BRACNH_NAME}

    ⚡ After merging it may give Conflict , which may be resolve in vs code


    • git diff --staged to see the difference after stage file
    • git diff {BRANCH1} {BRANCH2} also gives the diff b/w branches
    • git diff {LOG_ID1} {LOG_ID1} also gives the diff b/w log id's

    image.png


    Stash

    • git stash change branch without commit. (like temp storage)
    • git stash pop to bring back prev code.

    image.png


    More Command

    • git checkout {HASH} go to that commit files.
    • git switch main re-attach to Head.
    • git checkout HEAD~2 look at 2 commit prior
    • git restore {FILE_NAME} get back to last commit version

    Rebase

    🚫Never run this command in main or master branch

    • git rebase master join branch to master or other branch and clean up prev history

    image.png

    ⚡ 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 -v list of remote repo in git.

    • git remote {NAME} {URL} add remote file to locally.

    • git remote origin https://www.github.ksjnfannsdf.git mainly use in github.

    • git remote add origin https://www.github.ksjnfannsdf.git mainly 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 main push only one time

    • git push -u origin main or git push --set-upstream origin main setup an upstream that allow u to run future git push command and push code directly to github.

    image.png


    Open Sourced Contributions

    image.png

    1. First fork in repository so admin make eye on you.
    2. Clone to your machine.
    3. Make another Branch in git, don't interrupt main
    4. Done your work and the Push that branch into remote
    5. Back to GitHub and u well see Create Pull Request in site
    6. Add Title and Description (markdown syntax)
    7. 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