CSCI 2133 RAPID PROGRAMMING TECHNIQUES FOR INNOVATION Lab 02: Prep Lab: Basics of Git Instructor: Gang Liu Faculty of Computer Science Dalhousie University
Lab Overview ■ Creating repositories and sharing with TAs in future ■ Git cloning ■ Git history ■ Git branches ■ Git diff ■ Git merge and merge conflicts
Create repository on gitlab
Invite members to your gitlab repo Please add instructor, TA, marker to your repo and give maintainer role permissions.
Cloning the repository onto your local machine ■ Move to desired location on your system and CLONE your git repo with https for a local copy on your terminal >git clone <link> ( Please copy the link provided under “Clone with HTTPS” option ) <-Here
Add to git ■ Create a dummy txt file called as “csci2133.txt” in your local cloned git repository and add it to git using: >git add -A
Commit to git ■ Only changes that are committed to git will be recorded in the repository. So, to commit: >git commit –m “Initial commit”
Push changes to git ■ If you want the changes to reflect online on the gitlab master branch, you have to push it using the command: >git push origin master
Clone a github repository to understand some sample commands ■ An evil plan is on GitHub, you need to get a copy first ■ Here’s the https link: (DalCSS, 2016) https://github.com/DalCSS/GeekLabsGit.git ■ Can you clone the repository on your own? After cloning, please change directory to the local git repository
Git log ■ This shows the history of the repository and the commits made earlier to this branch. Open “map.txt” to understand where you are. >git log
Git checkout ■ If you want to move to a previous commit in the master branch, copy any one of the commit ids provided after “commit …” on git log. Check map.txt now. >git checkout 1bf609cdb9bc2b4872da38f0e88a9c1289fa17e4
Git checkout for moving to a different branch ■ Firstly check all branches available using: >git branch –a Then move to the branch using “git checkout branchname” >git checkout minion Where, minion is the branch name. Now check “map.txt” again to understand where you are.
Git diff ■ You can check differences between two commits across one file or multiple files using git diff >git diff commit_id1 commit_id2
Git branch concepts git checkout –b branchname This new branch will automatically contain all the content until the last commit made in the master branch before the creation of this branch. Branches run in parallel with the master and can contain code which is different from the master and then can be merged with the master using the command: >git checkout master Change to master branch >git merge branch_name Merge the new branch and its changes with the master branch >git push Push the changes to the repository
Git merge and merge conflicts ■ Scenario: There is a master branch which has a file “file.txt” with the numbers “1,2,3”. Programmer A and B create two branches “branchA” and “branchB” respectively. ProgrammerA checks out to his branch “branchA” and changes “1,2,3” in “file.txt” to “1,3,3” and pushes the changes to his branch and merges it with the master branch. ProgrammerB (still unaware of the changes made by ProgrammerA) changes “1,2,3” on his unchanged copy “file.txt” to “1,4,3”. He pushes it to his branch “branch” and when he switches to master and tries to merge it with the master branch, he hits on a roadblock called as “merge conflict”.
How to solve merge conflicts? ■ Pull the latest code before working on our branch everytime using: >git pull ■ Or when there is a merge conflict, the specific file can be opened on your local machine and git shows where the conflict is and it can be edited manually.
References ■ Some material in the lab tutorials are and will be borrowed from Dr. Vlado Keselj, the previous instructor of this course. ■ DalCSS. (2016). DalCSS/GeekLabsGit . [online] Available at: https://github.com/DalCSS/GeekLabsGit.git [Accessed 9 Jan. 2019].
Recommend
More recommend