17-Dec-2021
Git
What is the use?
1. Keep track of changes to code
2. synchronize code between different people
3. test the code without losing the old one
4. reverse back to the old version of code
How to use it?
1. repository ← where the code stores online
2. ppl will get access to the same repository
3. when you push the code back to server, the server will synchronize them
GitHub
website that stores repository online
Creating new repository
You need to download the repository into the computer so that you can add file into the repository by GIT. (SSH could be used but I don’t know how to use it)
First git command:
take the repository from internet to your computer
use cd to move into file hello
Git will not keep track on your change and update constantly.
You need to tell Git that you would like to save this version of repository→ called as a commit (taking snapshot of current position)
Second command:
Tell Git to add a file the next time I commit
Why need to separate the add and repository?
- let say there are 10 files, and you only want to update 3 of them. You can add only that 3 files and then the next commit will not update the 7 files.
***** YOU MUST ADD before commit *************
File is NOT yet saved
Third: git commit
message : notes on what changes you have made
be careful that there are no filenames included
→ file hello.html was not saved until commit was done
In this stage, you cannot find the file on GitHub as you haven’t upload the file to GitHub. → changes are happened locally only (you need to download the repository and you need to upload it.)
Forth command:
Telling you what happen inside the repository
The line “Your branch is ahead of “origin/master” by 1 commit ← telling you local file is differ from github version by 1 commit
Fifth command: git push
After pushing the repository to GitHub → It would be update!
Commit only once, only one branch was made
Below would be the file
Public → any one can download the repository and push their version onto it
After adding h1 to hello.html and saved it, if you check it through terminal
To do the ADD and COMMIT at the same time: git commit -am “message”
What if the version in GitHub is more updated than my local one?
git pull (take from git hub to local)
Minor tips: to change the file on GitHub, the branch must be MAIN
Using git pull
Merge Conflicts
When two people working on the same project, and the two version we are pushing are different → what should Git do?
You will receive message from Git
remote : the changes that are not matching with my push version
conflicting commit → the hash of conflicting commit in github (the code Git will generate every time you do the commit)
What to do?
remove the redundant part, keep the part that you want/ combine the two version
Exclamation point was added in the git version
Without refreshing the file in GitHub, we added color blue on h1 and commit it
Then I run git pull locally
There are conflicts messages and auto-merging is failed (as they are not on the same line), so you need to do it manually → open the file
After fixing it,
You may push it back to github
Then the problems are solved
Sixth command:
git log: keep track of changes/ commit made
Seventh command: git reset
Go back to previous commit
git reset --hard <commit> <-- reset everything from hashgit reset --hard origin/mast <-- the version of repository on git hub
Branching
master branch: default branch → up-to-date stable version
feature branch: working on some other feature
HEAD: the one that your repository are working on
When you finish → go to merge them
git branch: which branch you are on and how many branch there are
git checkout -b style : add a branch called style
git checkout <branch>: switch to the branch you want to go to
git merge <branch>: usually do on the main branch → the main branch will add a new commit with merging newest main + branch
If difference are not on the same line, auto merge will be done by git
GitHub — forking
https://github.com/twbs/bootstrap
Pull a copy of the repository and then change it til you are satisfied
Then you may send back to bootstrap through pull request, request your code being pulled and merged by bootstrap