CS 390 Software Engineering Lecture 5 More Git Reference: Scott Chacon and Ben Straub, Pro Git, published by Apress, available at https://git-scm.com/book/en/v2.
Outline Finish local repository Remote repository 2
Merging Branches To get a list of branches in the repository, use the branch command without any arguments. $ git branch Generally will want to merge branches back into the master branch. Need to be in the master branch, so check it out if not. $ git checkout master Then merge desired branch $ git merge <branch> If there are no conflicts, git will create a new commit node that has two parents. 3
Merge conflicts A merge conflict occurs when there are changes to a file in both branches. Git will not commit when this happens. For each conflict, git creates a single file with the conflicts from each branch marked. Developer manually edit the file and remove the markers. The modified files must be added and the project committed to make the resolution permanent. 4
Local project workflow Master branch is always a stable, working version. For each new feature or post-release bug fix, check out the master branch, then create a new branch. Or do both at the same time using -b option on check out. $ git checkout -b <branch> Commits of new code go to the branch. When new code passes unit testing, merged it back into the master branch. 5
Remote repository In order to share a git project, need a remote repository on a server. A remote repository usually is a bare repository. I.e., it is just a.git subdirectory without project files. Developers clone a remote repository into a working directory. $ git clone <url> URLs indicate what protocol is used to get the repository. We will be using SSH to access write-all remote repositories (created by instructor) on csserver. $ git clone ssh://[user@]csserver.evansville.edu/usr/local/<project> 6
Remote repository example A practice repository has been set up on csserver $ git clone ssh://[user@]csserver.evansville.edu/usr/local/git/example.git This will ask for a password unless you have set up a public key. See Section 4.3 of Pro Git book. 7
Remote repository In the (local) working directory, refer to the remote repository branch using <remote>/<branch>. The default remote name after cloning is 'origin', so the most common remote branch name is origin/master. Working directory changes are committed to the (local) repository. A clean working directory can be pushed to a remote repository branch. $ git push <remote> <branch> 8
Remote repository Push fails if remote branch has been modified. Requires synchronization. Can be done using fetch, then merge. Fetching does not change the local repository, so can be done at any time to update the remote branch heads. $ git fetch <remote> $ git merge <remote>/<branch> Synchronization can also be done using pull, which is basically fetch and merge in one step for current branch. $ git pull 9
Remote repository example Change to the example working directory Create a file named <username>.txt and write a message in it. Add the file and commit it to the (local) repository. Push changes back to remote repository. If need to synchronize, do so, and try again. 10
Remote repository example Add a line to README file prefixed with username. Add and commit change to (local) repository. Attempt to push changes. If rejected for conflicts, merge with origin/master, remove conflict markers and try again. 11
Workflows Centralized workflow One shared repository that everyone clones and pushes to. Need to coordinate changes so they are compatible Integrated manager workflow The project maintainer pushes to their public repository. A contributor clones that repository and makes changes. The contributor pushes to their own public copy. The contributor requests the maintainer to pull changes. The maintainer adds the contributor s repository as a remote and merges locally. The maintainer pushes merged changes to the main repository. 12
Workflows Dictator and lieutenants workflow Regular developers work on their topic branch and rebase their work on top of master. The master branch is that of the reference repository to which the dictator pushes. Lieutenants merge the developers' topic branches into their master branch. The dictator merges the lieutenants' master branches into the dictator s master branch. Finally, the dictator pushes that master branch to the reference repository so the other developers can rebase on it. 13