Introduction
This is not Java but several newbie developers ask the same question, how to use GIT and how does GIT works so here it goes…
Have you ever work in SVN? Well forget everything you know and lets start over 🙂
What is a GIT repo?
In general there are two mirror repos. Your local repo and the remote repo. Yes TWO REPOS. Everyone in the team has an actual copy of the whole repo so even if your remote server dies you can set it up again and just push (spoiler) your repo to the repote server.
Your local repository consists of three “trees” maintained by git:
- The Working Directory which holds the actual files. The
- Index which acts as a staging
- The HEAD which points to the last commit you’ve made.
So lets begin the crash course…
To create a new git repository.
#Start a git repository in the particular path git init
checkout a repository
#Create a working copy of a local repository by running the command git clone /path/to/repository #When using a remote server, your command will be git clone username@host:/path/to/repository
Add & commit
#Shows current branch status and lists files that have changes. #First it lists files in the Stage (to be committed) #And below go the files that are not staged. git status #You can propose changes (add it to the Index) using git add #Or if you want to add everything git add . #Or even git add --all #To actually commit these changes use git commit -m "An awesome commit message :p" #If a file was added to the stage by mistake undo a stage git reset particularFile.extension #Reseting code by deleting all changes. HEAD is the latest local commit git reset --hard HEAD #Same as above but N commits back. git reset --hard HEAD~N #Reset code but keep changes locally. (usefull for uncommiting a not pushed mistake) #Use HEAD~2 if a merge occured. git reset --soft HEAD~1 #Save local uncommitted changes temporarily git stash #Bring saved changes back git stash apply
Check what has happened while you were away…
#Use to see changes committed. Commit it can be found by git log git show --pretty="" --name-only [commitID] Pushing changes #Your changes are now in the HEAD of your local working copy. To send those changes to your remote repository in your desired branch, execute git push origin Branching
Update & merge
#You can study repository history using and get commit id if needed git log
How to discover a bug using git bisect
This thing is broken!
It worked last week!
What Happened? Can we pin point the code that broke it!
Yesss we have all had this conversation in the past…
Assuming we have Good Commit Messages (that’s another post subject but lets carry on) we ‘ll use git bisect.
You provide git bisect two points, a GOOD point in time and BAD point in time. Lets asume that these are the commits HEAD and 134245634bkl2730bc5der. It bisects time and gives you the commit in between. It the code is OK there you mark it as GOOD, else you mark it as BAD. After some iterations you land on the commit that caused the issue. Check the example bellow
git bisect start HEAD 134245634bkl2730bc5der Bisecting: 4 revisions left to test after this (roughly 2 steps) [3453lj45b3ljhrgo2ug42bbh98112] Revert "Refactored hello() method to pass sonar" #test your code here git bisect bad #or use a script that exits 1 Bisecting: 2 revisions left to test after this (roughly 1 step) [x7y435h34r87yhndfdsf0dsfw3452] Added some nice staff I found on the web git bisect good #or use a script that exits 0 Bisecting: 0 revisions left to test after this (roughly 0 steps) [234fy435h45b09jdfdsf0dsfw3452] Added ability to fly like superman git bisect bad 234fy435h45b09jdfdsf0dsfw3452is the first bad commit commit 234fy435h45b09jdfdsf0dsfw3452 Author: Alexius alexius@example.com Date: Sat Oct 12 15:40:46 2019 Added ability to fly like superman bisect run success
That all the info you need on GIT!
Enjoy!