Git A distributed version control system 20 Oct21
Git A distributed version control system 20 -Oct-21
Version control systems n Version control (or revision control, or source control) is all about managing multiple versions of documents, programs, web sites, etc. n n n Some well-known version control systems are CVS, Subversion, Mercurial, and Git n n n Almost all “real” projects use some kind of version control Essential for team projects, but also very useful for individual projects CVS and Subversion use a “central” repository; users “check out” files, work on them, and “check them in” Mercurial and Git treat all repositories as equal Distributed systems like Mercurial and Git are newer and are gradually replacing centralized systems like CVS and Subversion 2
Why version control? n For working by yourself: n n For working with others: n n But it was just working a moment ago! Gives you a “time machine” for going back to earlier versions Gives you great support for different versions (standalone, web app, etc. ) of the same basic project Greatly simplifies concurrent work, merging changes For getting an internship or job: n n Any company with a clue uses some kind of version control Companies without a clue are bad places to work 3
Who is this? Linus Torvalds He’s the guy behind • Linux • Git 4
Why Git? n Why git? n n n n The ability to undo changes A complete history of all the changes Documentation of why changes were made The confidence to change anything Multiple streams of history Independent streams of history The ability to resolve conflicts Why Git. Hub? n n n Can document bugs and specify new features Collaborate on independent streams of history Review work in progress See team progress Plus, it’s the most well-known and popular repository Taken mostly from Introducing Git. Hub, by Peter Bell and Brent Beer 5
Download and install Git n n n There are online materials that are better than any that I could provide Download and documentation here: http: //git-scm. com/downloads Here’s documentation from Stack. Exchange: http: //stackoverflow. com/questions/315911/git-for-beginners-thedefinitive-practical-guide#323764 Note: Git is primarily a command-line tool and my goal is to teach you the main commands There are GUIs n n n Git. Hub has clients for windows and macs (Google for Git. Hub Desktop) Smart. Git, also with tutorials (ridiculous URL, just Google for it) Tortoise git (https: //code. google. com/p/tortoisegit/) 6
Getting started using Git. Hub n n n http: //www. cis. upenn. edu/~matuszek/cit 5942015/Pages/Git_with_Git. Hub_On_SEAS. pdf Create an account on Git. Hub You may also need to create SSH keys n https: //help. Git. Hub. com/articles/generating-ssh-keys Alternative to Git. Hub n Bitbucket n Free private code hosting for up to 5 users on a project 7
Create your first repository n n Log in to Git. Hub Click on create new repository Public repository is the only unpaid option Click on ‘create readme …’ n This makes it cloneable (explained later) 8
Introduce yourself to Git n n Do these in the bash command shell (git bash) Enter these lines (with appropriate changes): n n git config --global user. name "John Smith" git config --global user. email jsmith@seas. upenn. edu You only need to do this once If you want to use a different name/email address for a particular project, you can change it for just that project n n cd to the project directory Use the above commands, but leave out the --global 9
Create and fill a repository Feel free to skip this if you want to clone your Git. Hub repository 1. cd to the project directory you want to use 2. Type in git init n n This creates the repository (a directory named. git) You seldom (if ever) need to look inside this directory 3. Type in git add. n The period at the end is part of this command! n n Period means “this directory” This adds all your current files to the repository 4. Type in git commit –m "Initial commit" n You can use a different commit message, if you like 10
Clone a repository from elsewhere n n n git clone URL Here’s an example URL to a project https: //github. com/David. Matuszek/Sentence. Gener ator git clone URL mypath n These make an exact copy of the repository at the given URL git clone git: //Git. Hub. com/rest_of_path/file. git n Git. Hub is the most popular (free) public repository n All repositories are equal n n n But you can treat some particular repository (such as one on Git. Hub) as the “master” directory Typically, each team member works in his/her own repository, and “merges” with other repositories as appropriate 11
git add versus git commit n n n git add ‘stages’ things for a commit git add. will add all your changes If you want to split your commits up git add filename You might also want to look into n n git add –i which is the interactive addition option At the point of writing a git commit, all that was added will turned into a commit 12
Why add and commit? n n n Add progressively Most people check to make sure they are adding the right set of files If you are working on a big fix that involves multiple files (remove the print statement in all my files for instance) n n n you do git add at each stage where a particular file was fixed Stages all your ‘cleaned up’ files for a commit You make one commit involving all those files Your collaborators value you if each of your commits is an independent unit of work Think of a commit as a ‘software patch’! Remember how annoying it can be if you need to apply multiple patches to get something to work… 13
Fixing a typo in a commit … n n n git commit –amend Weird editor opens up! Vim! (http: //www. vim. org/) n n n You really only need to know some simple commands Esc + ‘a’ - append Esc + ‘i’ - insert Esc + ‘: wq’ – save and quit You can set the default editor; I like Sublime Text n git config --global core. editor "'c: /program files/sublime text 3/sublime_text. exe' –w" 14
The repository n Your top-level working directory contains everything about your project n n n At any time, you can take a “snapshot” of everything (or selected things) in your project directory, and put it in your repository n n The working directory probably contains many subdirectories—source code, binaries, documentation, data files, etc. One of these subdirectories, named. git, is your repository This “snapshot” is called a commit object The commit object contains (1) a set of files, (2) references to the “parents” of the commit object, and (3) a unique “SHA 1” name Commit objects do not require huge amounts of memory You can work as much as you like in your working directory, but the repository isn’t updated until you commit something 15
init and the. git repository n When you said git in your project directory, or when you cloned an existing project, you created a repository n n The repository is a subdirectory named. git containing various files The dot indicates a “hidden” directory You do not work directly with the contents of that directory; various git commands do that for you You do need a basic understanding of what is in the repository 16
Making commits n n You do your work in your project directory, as usual If you create new files and/or folders, they are not tracked by Git unless you ask it to do so n n Committing makes a “snapshot” of everything being tracked into your repository n n git add new. File 1 new. Folder 2 new. File 2 A message telling what you have done is required git commit –m “Uncrevulated the conundrum bar” git commit n This version opens an editor for you the enter the message n To finish, save and quit the editor Format of the commit message n n One line containing the complete summary If more than one line, the second line must be blank 17
Commits and graphs n n A commit is when you tell git that a change (or addition) you have made is ready to be included in the project When you commit your change to git, it creates a commit object n n n A commit object represents the complete state of the project, including all the files in the project The very first commit object has no “parents” Usually, you take some commit object, make some changes, and create a new commit object; the original commit object is the parent of the new commit object n n You can also merge two commit objects to form a new one n n Hence, most commit objects have a single parent The new commit object has two parents Hence, commit objects form a directed graph n Git is all about using and manipulating this graph 18
How to visualize the git graph n Use gitk n n Actually type ‘gitk&’ so that you can still work on the git shell Use a log statement git log gives you most all information git log --stat gives you information about what files changed n Useful when you tracking down the ‘bad’ commit 19
Working with your own repository n n n A head is a reference to a commit object The “current head” is called HEAD (all caps) Usually, you will take HEAD (the current commit object), make some changes to it, and commit the changes, creating a new current commit object n n You can also take any previous commit object, make changes to it, and commit those changes n n This results in a linear graph: A B C … HEAD This creates a branch in the graph of commit objects You can merge any previous commit objects n This joins branches in the commit graph 20
Commit messages n n In git, “Commits are cheap. ” Do them often. When you commit, you must provide a one-line message stating what you have done n n Terrible message: “Fixed a bunch of things” Better message: “Corrected the calculation of median scores” Commit messages can be very helpful, to yourself as well as to your team members You can’t say much in one line, so commit often 21
Choose an editor n n When you “commit, ” git will require you to type in a commit message For longer commit messages, you will use an editor The default editor is probably vim To change the default editor: n n git config --global core. editor /path/to/editor You may also want to turn on colors: n git config --global color. ui auto 22
Working with others n n All repositories are equal, but it is convenient to have one central repository in the cloud Here’s what you normally do: n n n Download the current HEAD from the central repository Make your changes Commit your changes to your local repository Check to make sure someone else on your team hasn’t updated the central repository since you got it Upload your changes to the central repository If the central repository has changed since you got it: n It is your responsibility to merge your two versions n n This is a strong incentive to commit and upload often! Git can often do this for you, if there aren’t incompatible changes 23
Typical workflow n git pull --rebase n n n git status n n n Get changes from a remote repository Put your current changes on top of those changes See what Git thinks is going on Use this all the time!! We used this in my company often enough that one person spent a day speeding it up by 50% and was hailed as a hero! Work on your files When you achieve something or you get to a working version and you want to commit 24
Typical workflow n git add. n n n git commit –m “What I did” n n git add specific_filename git add –i (personal favourite) Please make meaningful commit messages git difftool n Examine changes before committing n If you want to ‘undo’ mistaken commit: git reset n git push origin master n It is important to say which ‘branch’ you are pushing to 25
Git objects n n You can refer to pretty much any git ‘step’ by its SHA – secure hash algorithm http: //git-scm. com/book/en/Git-Internals-Git-Objects Read it only if you are interested in internal workings of git n Mostly used by git admins but rarely (fingers-crossed) by others 26
Going back in history ‘Can somebody tell me how to get things back The way they used to be ’ git checkout <sha> You will get an amusing message about detached head etc. Be careful to come back to ‘present’ git checkout master 27
Not ready for a commit? n n n git stash git pull -- rebase git stash pop or git stash apply n Pop will pop the stash from your stack of stashes 28
Multiple versions (using branching) Initial commit Second commit Third commit Bob gets a copy Fourth commit Bob’s commit Merge 29
Keeping it simple n If you: n n Then you don’t have to worry about resolving conflicts or working with multiple branches n n All the complexity in git comes from dealing with these Therefore: n n n Make sure you are current with the central repository Make some improvements to your code Update the central repository before anyone else does Make sure you are up-to-date before starting to work Commit and update the central repository frequently If you need help: https: //help. Git. Hub. com/ 30
Resolving conflicts n n n If you see a message like ‘blabla is a conflict, you cannot commit or you cannot push’ This means someone else modified the same lines How to resolve these conflicts? n Manually n n Not too bad usually p 4 Merge is the tool of choice Smart. Git has an interface for this. Be careful about which code is yours and which code is ‘theirs’ 31
Extra extra topics None of these will usually be needed but you might want to google n Branching n n n Very useful if you are afraid of affecting the main branch of code Interactive rebasing Git bisecting 32
The End When I say I hate CVS with a passion, I have to also say that if there any SVN [Subversion] users in the audience, you might want to leave. Because my hatred of CVS has meant that I see Subversion as being the most pointless project ever started. The slogan of Subversion for a while was "CVS done right", or something like that, and if you start with that kind of slogan, there's nowhere you can go. There is no way to do CVS right. --Linus Torvalds, as quoted in Wikipedia 33
Appendix showing screenshots to get basic git up and running via Git. Hub 34
35
Log in to Git. Hub 36
37
38
Copy that clone URL into git bash 39
Getting ready 40
Initial commit 41
I just copied a Java project to this directory (battleship assignment from 590/591) 42
git add Do a git add and then notice all the files are staged for a commit 43
Commit and push 44
commit I made some changes in the comments for Destroyer. java 45
Beyond compare n n n Beyond compare is a great tool to use to make sure you know what you will commit http: //stackoverflow. com/questions/2069490/git-diff-with-beyond-compare Once beyond compare is set up, git difftool will load each diff into beyond compare 46
Text comparison Differences between the files 47
48
gitk visualization 49
Checking out previous versions 50
Adding collaborators Add other collaborators to your code. They should be able to pull and push code now 51
The End 52
- Slides: 52