Git Flashcards
Git Commit
Git repository not keeps track of every character written. Commit command tell git to save the current state of every changed and take snapshot for user to be able to refer to back this current state in future. Two steps:
- git add “filename”/”.”:
Tell git what file to save/keep track/take snapshot of in the next commit. If not add the file won’t be tracked despited change so called modification not staged for commit
- git commit -m “comment”:
Actually save the current state of chosen file and take a snapshot.
Short hand: git commit -am “comment” add all the file changed and commit. The commit directly on github pages equivalent to the shorthand
git status
git log
“git status” to show how what happen on repository respected to old version:
- Current branch (ex: or )
- Is local repository is ahead or behind remote one, how many commits.
“git log” to show all the commit snapshot and hash code for reference
git push
Apply commit changes done in local repository into the Github remote repository, sometimes causing conflicts
git pull
Take changes recently on Github repository and apply those changes to the local repository
Merge conflicts
Git makes changes only by removing and inserting lines of code.
If local changed apply on old version git repository on the same line with remote changed happen in the newest update of repository, when trying to pull from github (to be able to edit on the newest version of the projects), conflicts will happen, git automatic merge fail and add metadata to conflict line and let programmer to decide final changes then push to the remote github and continue the process fluid. Ex:
a = 1
««««_space;HEAD (Current commit on local)
b = 2
======== (Commit happened in remote that forgotten to pull to local before making change on the above repo)
b = 0
»»»» (Hash code of the conflict commit)
c = 3
Decide final change push to github then continue edit other part. (Clear the metadata alert, keep the wanted line or edit for apply both change)
a = 1
b = 99
c = 3
Then push conflict solved, continue to work on other parts
git reset
git reset –hard
Take the current local repository, revert back to the old version of the chosen commit (–hard: change every file)
git reset –hard origin/master
Revert the current local repository back to the commit version saved in branch master in the remote Github repository
Branching
-Context: A developing stable projects need a new feature -> add new features next commit and modify for some more commits, then to realize at the step before adding features haves some bugs that relate to the new features -> tricky situation: have to abandoned features, revert back stable and fix bug.
-Conclusion: Just commit every change on one branch is linear style development having some drawbacks ex: having to work one project after another causing bottlenecks
-Git branch: Way to working simultanous on multiple parts of a projects. Ex: on one stage of development create branches for adding some features, separate from the stable code, so if recognize some bugs, just checkout to master to fix then keeps working on features, after that merge and solving conflict between branches. -> final projects
HEAD: the current branch
Checkout
git checkout (-b) “branch-name”
Command for jumping between branches, or creating and jumping new branches
git branch
Show all branches and indicate the current branch
Merging Branch
Using “git branch” command to show current branch. Ex:
style
*master
Current branch is master.
The merging process is choosing one in the other branches, merging into the current branch (master in example) and destroy that branch, sometimes will occur merge conflicts both branches have modified same line in the branching period
Branching conflict example
index.html (master): < head > < title > Hello! < / title > < / head > < body > < h1 style="color:blue;"> Welcome !! < / h1 > Hello, worlds! < / body >
git branch -b style
edit index.html (style): < head > < title > Hello! < / title > < style > h1 { color: blue; } < / style > < / head > < body > < h1 > Welcome !! < / h1 > Hello, worlds! < / body >
But realize that need to delete one punctuation in the stable version (master)
git branch master
edit index.html (master): < head > < title > Hello! < / title > < / head > < body > < h1 style="color:blue;"> Welcome ! < / h1 > Hello, worlds! < / body >
Satisfy with style branch, current branch is master, wanting to merge style into master and delete “style” branch. In master branch:
git merge style
Conflict Alert since there is one line that changed by both branched during branching period. Now the style branch has gone, coming back to index.html and solving the conflicted line git has marked for helping the solve
«««_space;HEAD (current change) / Change in current (master) branch /
< h1 style=”color:blue;”> Welcome ! < / h1 >
====== / Change in the incoming (style) branch /
< h1 > Welcome !! < / h1 >
»»» style (incoming change)
So programmers will handle this process, deleting markers and edit the final wanted code
< h1 > Welcome ! < / h1 >
Then git commit -am “merge conflict solved”