Software Development

Git Essentials Crash Course

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.

1
2
#Start a git repository in the particular path
git init

checkout a repository

1
2
3
4
5
#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 [email protected]:/path/to/repository

Add & commit

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#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…

1
2
3
4
5
#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

1
2
#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

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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 [email protected]
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!

Published on Java Code Geeks with permission by Alexius Diakogiannis, partner at our JCG program. See the original article here: Git Essentials Crash Course

Opinions expressed by Java Code Geeks contributors are their own.

Alexius Diakogiannis

Author of JEE.gr, JEE Architect, Scrum Master, Enthusiastic Entrepreneur, Passionate Archer, Linux Lover and JAVA Geek!
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button