A Short Introduction To Git
A Short Introduction To Git
Version control system: Tool that manages changes made to files in a project. Git strengths are:
•Nothing that is saved to Git is ever lost, so you can always go back to see which results
were generated by which versions of your programs.
•Git automatically notifies you when your work conflicts with someone else's, so it's
harder (but not impossible) to accidentally overwrite work.
•Git can synchronize work done by different people on different machines, so it scales as
your team does.
Repository:
• Proyect files.
• Git records about project’s history. -> .git in proyect root
Console commands:
• “git“
◦ “ anotate “file”” - Shows who made the last change to each line of a file and when.
◦ “ commit” - Saves everything in the staging area as one unit.
▪ “ -m” - Log message - “Sample”
▪ By default, opens a text editor with a template.
• # this is a comment
◦ “ diff” - Formatted display of the differences between two sets of files.
▪ “ filename”. - File to show changes.
▪ “ -r” - Compare to a particular revision.
• “ HEAD” - shortcut meaning "the most recent commit".
◦ “~1” - commit before head – change “1” for “n” to keep going back.
▪ “ ID1. .ID2” - See the changes between two commits.
◦ “ log” - Displays proyect history of changes.
▪ “ path” - Inspect only the changes to particular file or path.
◦ “ show” - Displays the details of the commit with the specified hash.
▪ “ [Part of a hash]”
◦ “ status” - Dispays changes to be commited
•The command used to produce the output (in this case, diff --git). In it, a and b are
placeholders meaning "the first version" and "the second version".
•An index line showing keys into Git's internal database of changes. We will explore
these in the next chapter.
•--- a/report.txt and +++ b/report.txt, which indicate that lines being removed are
prefixed with -, while lines being added are prefixed with +.
•A line starting with @@ that tells where the changes are being made. Here, the line
shows that lines 1-4 are being removed and replaced with new lines.
•A line-by-line listing of the changes with - showing deletions and +showing additions.
(We have also configured Git to show deletions in red and additions in green.) Lines
that haven't changed are sometimes shown before and after the ones that have in
order to give context; when they appear, they don't have either + or - in front of them.
Desktop programming tools like RStudio can turn diffs like this into a more readable side-by-
side display of changes; you can also use standalone tools like DiffMerge or WinMerge.
Hash
Every commit to a repository has a unique identifier called a hash (since it is generated by
running the changes through a pseudo-random number generator called a hash function). This
hash is normally written as a 40-character hexadecimal string. Git compares hashes, not entire
files.
Text editors:
• “nano”
◦ “ filename” - Opens or creates file for editing.
◦ ControlKeys:
▪ Ctrl-K: delete a line.