2019-05-01_Linux_Format

(singke) #1

90 LXF249 May 2019 http://www.linuxformat.com


CODING ACADEMY Keep track of Git


your repository.
General confusion led to the decision that it would be
useful to have a single command (a Bash shell script)
that would summarise the status of the repository of
the seven source code directories and would indicate
which directories need attention.
The image above shows the output of the shell
script, which we’ve called gitStatus. The green items
are up to date and need no attention. The yellow and
red items do need attention. They tell you that the
utility directory has one or more untracked files:
source code files that were never put under version
control. That may not be a problem so we indicate
that condition in yellow – as in caution.
They also tell you that the transport source code
directory has two files on disk that are different from
the versions under source control. Two files in the

itisa versioncontrolsoftwaresystemthat
keepstrackofallofthechangesyou’vemade
toyoursourcecodeandenablesyoutorevert
toa previousversionatanytime.It’simportanttokeep
Gituptodatebycheckingin(committing)yoursource
codefrequently– wheneveryouhavemadechanges
thatyou’vetestedandarehappywith.Thisis
particularlyimportantincontinuousintegration/
deploymentenvironments.
Thisgit-checkingscriptstemmedfroma current
C++projecthassevendifferentsourcedirectories,with
a singleGitrepositorythatspansallthedirectories.To
puttheseprojectsunderGitsourcecontrol,we’dopena
terminalintheproject’sbasedirectory,~/
Development/UDS(theultimateparent
ofallofthesourcedirectories)andissuetheGitinit
command.ThismakesthebasedirectoryaGit
repository.Thengoingtoeachoftheprojectsource
directoriesissueGitadd* tostagethefiles.Finally,
goingbacktotheprojectbasedirectoryandissue
Git commit -am “initial version” tocommitallthe
staged versions.
If you have certain artifact files that show up in all
your source directories, you can tell Git to ignore them
and not treat them as untracked files. The Eclipse IDE
places a .cproject file, a .project file and a .settings
directory in each of the source directories. You can
create a .Gitignore file containing the names of these
items and place it in the Git repository directory.
When you’re working in many different directories at
the same time, it’s easy to get confused and forget just
what you’ve changed and what you haven’t. Git enables
you to go to each of your source code directories, one
at a time, and use the Git status. command to tell you
what hasn’t yet been committed to the repository and
whether there are any new files in the directory that
haven’t yet made it into source control.
You can also use the Git diff command to tell you
exactly what has changed between a source file in the
directory and a source file in the Git repository. To
commit your changes to Git, use the command
Git commit filename -am “Your comment goes here”.
Meanwhile, the gitk command shows you a GUI view of

The output from the gitStatus shell script is designed to be instantly
readable, with the use of colour coding.

Check the status


of your Git repos


John Schwartzmanshowsyou how to write a Bash shell script that


cankeeptrackofallofyourmany Git repositories.


John
Schwartzman
is a software
engineering
consultant to
business and
government.
He also teaches
Computer
Science at a
local college.


G


SHELL SCRIPTS


OUR
EXPERT

Gitisn’tonlyfor
C++projects.
Youmayhave
Perl,Python,
C,shellscripts,
textfiles,
makefiles,
configuration
filesor
magazine
articlesthat
youwantto
keeptrackof.
Youprobably
alreadyhave
Gitinstalledon
yoursystem.
Trytypinggit
--versionatthe
commandline.
Free download pdf