Git Basics

Git Basics

By Ifeanyi Omeata


Here are the basic topics:


1. Get Git Version
2. Set Global Configurations
3. Get Help
4. List files
5. Create new folder (mkdir)
6. Initialize git from existing code
7. Stop tracking/ remove git from code
8. View git status
9. Create and add to .gitignore file
10. Add files to Staging Area
11. Remove files from Staging back to Working Directory
12. Commit files from Staging Area to Repository
13. View History of Commits
14. Clone a Remote Repository
15. View Info about cloned Remote Repository
16. View changes made to code
17. Pull updates from remote repository
18. Push updates from local to remote repository
19. View existing branches
20. Create new branch
21. Switch to Branch
22. Merge branch to Master and view merges
23. Delete branch
24. Simple Git Workflows
25. Abort merge during conflict
26. Save Stash to background
27. View Stash list
28. Restore Stash from background
29. Remove Stash from background
30. Display git commit History
31. Rename File/Move File
32. Change last commit
33. Change date of last commit
34. Change old typo
35. Change to first version of commit
36. Reset and split commits
37. View last two commits
38. Resolving multiple commits
39. Granting Permissions
40. Splitting Commit Parts
41. Cherry Pick features
42. Change order of commits
43. Find commits with swearwords
44. Find commit that introduced bug


1. Get Git Version


>>Return to Menu

git --version

image.png


2. Set Global Configurations


>>Return to Menu

Global Config:

git config --global user.name "<Your Name>"
git config --global user.email "<Your Email>"

List Global Configurations:

git config --list

git config user.email

image.png


3. Get Help


>>Return to Menu

git help <verb>
git <verb> --help

git help commit
git commit --help

image.png


4. List files


>>Return to Menu

ls
ls -lrt
ls -la

image.png


5. Create new folder (mkdir)


>>Return to Menu

mkdir practice

image.png


6. Initialize git from existing code


>>Return to Menu

git init

image.png


7. Stop tracking/ remove git from code


>>Return to Menu

rm -rf .git

image.png


8. View git status


>>Return to Menu

git status

image.png


9. Create and add to .gitignore file


>>Return to Menu

touch .gitignore

image.png

Add Exceptions:

echo questions.py > .gitignore
echo *.exe >> .gitignore
echo libraries/ >> .gitignore

image.png

View .gitignore file:

cat .gitignore
git status

image.png


10. Add files to Staging Area


>>Return to Menu

git add -A
git add --all
git add .

git add A.txt

image.png


11. Remove files from Staging back to Working Directory


>>Return to Menu

git reset

git reset A.txt
git restore --staged A.txt

image.png


12. Commit files from Staging Area to Repository


>>Return to Menu

git commit -m "Initial commit"
git commit -am "Initial commit"

image.png


13. View History of Commits


>>Return to Menu

git log

image.png


14. Clone a Remote Repository


>>Return to Menu

git clone <url> <where-to-clone>

git clone https://github.com/omeatai/practice-app.git ./cloud

image.png


15. View Info about cloned Remote Repository


>>Return to Menu

View general Information:

git remote -v

image.png

View branch Information:

git branch -a

image.png


16. View changes made to code


>>Return to Menu

git diff

image.png


17. Pull updates from remote repository


>>Return to Menu

git branch -M main
git remote add origin https://github.com/omeatai/practice-app.git

git pull origin main
<git pull origin master>
<git pull origin question-1>

image.png


18. Push updates from local to remote repository


>>Return to Menu

git remote add origin https://github.com/omeatai/practice-app.git (add remote repo)
git remote set-url origin https://github.com/omeatai/practice-app.git (setting to a remote repo)
git remote -v (check if repo is set)
git branch -M main (set primary branch)
git push --set-upstream origin main
git push -u origin main
git push origin main

image.png


19. View existing branches


>>Return to Menu

git branch

image.png


20. Create new branch


>>Return to Menu

git branch calc-divide

image.png


21. Switch to Branch


>>Return to Menu

git checkout calc-divide

image.png


22. Merge branch to Master and view merges


>>Return to Menu

git merge calc-divide 

git branch --merged

image.png


23. Delete branch


>>Return to Menu

Delete local branch:

git branch -d calc-divide

git branch -a

image.png

Delete remote branch:

git push origin --delete calc-divide

image.png


24. Simple Git Workflows


>>Return to Menu

git branch subtract
git checkout subtract
#make changes to code
git status
git add -A
git commit -m "Subtract function"

image.png

git push -u origin subtract
git checkout master
git pull origin main
git merge subtract
git push -u origin master

image.png

git branch
git branch -d subtract
git branch

image.png

git push origin --delete subtract
git branch -a

image.png

Set Config Values and Push to remote:

git clone https://gitexercises.fracz.com/git/exercises.git
cd exercises
git config user.name <your.name>
git config user.email <your.email>

git push -u origin question-1


25. Abort merge during conflict


>>Return to Menu

git merge --abort


26. Save Stash to background


>>Return to Menu

git stash
git stash save "worked on functions"
git diff
git status


27. View Stash list


>>Return to Menu

git stash list
git stash show


28. Restore Stash from background


>>Return to Menu

git stash pop
git stash apply stash@{0}


29. Remove Stash from background


>>Return to Menu

git stash clear
git stash drop stash@{0}


30. Display git commit History


>>Return to Menu

git log
git log --oneline
git reflog


31. Rename File/Move File


>>Return to Menu

git mv File.txt file.txt
git commit -am "renamed file"


32. Change last commit


>>Return to Menu

git rebase -i
#choose edit
git commit --amend
git rebase --continue

git commit -a --amend


33. Change date of last commit


>>Return to Menu

git commit --amend --date 1-1-1987


34. Change old typo


>>Return to Menu

git rebase -i HEAD~2
#edit
git add file.txt
git rebase --continue

git rebase -i HEAD^^
# mark the first commit with "edit" command
# fix the typo in the file
git add file.txt
git rebase --continue
# fix the typo in the commit message


35. Change to first version of commit


>>Return to Menu

git reflog
git reset --hard HEAD@{1}


36. Reset and split commits


>>Return to Menu

git reset HEAD^
git add first.txt
git commit -m "First.txt"
git add second.txt
git commit -m "Second.txt"

git reset HEAD~1
git add first.txt
git commit -am "First commit"
git add second.txt
git commit -am "Second commit"


37. View last two commits


>>Return to Menu

git log -2


38. Resolving multiple commits


>>Return to Menu

git rebase -i HEAD^^
# "squash" or "fixup" the second commit


39. Granting Permissions


>>Return to Menu

git update-index --chmod= +x script.sh
git add --chmod +x script.sh
git commit -m "add chmod executable permissions"


40. Splitting Commit Parts


>>Return to Menu

git add -p file.txt
# choose lines to include with 'y'
git commit -m "First part of changes"
git commit -am "The rest of the changed"

git add -p
git commit -m "task 1"
git add -p
git commit -m "task 2"


41. Cherry Pick features


>>Return to Menu

git cherry-pick feature-a
git cherry-pick feature-b
git cherry-pick feature-c
# resolve merge conflict
git add -A
git cherry-pick --continue


42. Change order of commits


>>Return to Menu

git rebase -i HEAD~2
# move the second commit up


43. Find commits with swearwords


>>Return to Menu

git log -Sshit
git rebase -i <found commit>^
# remove the swearword
git rebase --continue


44. Find commit that introduced bug


>>Return to Menu

git bisect start HEAD 1.0 --
git bisect run sh -c "openssl enc -base64 -A -d < home-screen-text.txt | grep -v jackass"
git push origin 87ed21023676610889c849b8b4b8e62e78a3f9ae:find-bug

git bisect start
git bisect bad HEAD
git bisect good 1.0
git bisect run sh -c "openssl enc -base64 -A -d < home-screen-text.txt | grep -v jackass"

#End


Hope you enjoyed this! :) Follow me for more contents...


Get in Touch:
ifeanyiomeata.com

Youtube: youtube.com/c/IfeanyiOmeata
Linkedin: linkedin.com/in/omeatai
Twitter: twitter.com/iomeata
Github: github.com/omeatai
Stackoverflow: stackoverflow.com/users/2689166/omeatai
Hashnode: hashnode.com/@omeatai
Medium: medium.com/@omeatai
© 2022