Prepared by: Nasseef Abukamail (abukamai@ohio.edu)
Version control is the management of changes of projects (code) overtime. It is essential in team projects. As changes are made to a project, these changes need to be tracked. The collection of the project and their changes overtime is called a repository (repo).
A repository includes a chronological record of all the changes made to a project. The repositories need to be stored on a server to be shared between team members. Services like GitHub are used to store repositories. Alternatively, an organization can set up their own servers to maintain their repositories.
Git is a free and open source tool (collection of commands) that is used for version control. It is the most commonly used version control system in the industry. It allows for easy management of individual and team projects. It will allow multiple people to work on the same project without worrying about their changes being overwritten (lost).
Make sure you have a GitHub account, and your Personal Access Token has been setup.
-
Create the project directory. Open a terminal window or a PowerShell window and issue the commands:
mkdir git-lab cd git-lab
-
Create two empty (Markdown files)
README.mdandanswers.md. Markdown is similar to html but much simpler. Search online for a quick reference. Record all your answers for this lab inanswers.md.-
For WSL/Mac/Linux issue the commands:
touch README.md touch answers.md
-
For Windows computers issue the command:
echo "" >> README.md echo "" >> answers.md
-
-
In a terminal window find out what version of git is installed on the system.
git --versionRecord the output of the command as
Answer 1. -
Git needs to know your credentials (name and email). Issue the commands:
git config --global user.name "Your name" git config --global user.email "Your email"
You can verify the changes by issuing the command:
git config --listRecord the output of this command as
Answer 2. -
Git includes a comprehensive help system. You can invoke help by issuing the following command
git <command> --helpreplace
<command>with any git command. Try the following:git add --helpWhat happens when you type
git --help? Record your answer asAnswer 3.
Creating and maintaining a repository
-
In a terminal window go to the
git-labdirectory.cd git-labWe will use the directory
git-labas our local repository. Issue the command:ls -a (Mac/Linux/WSL) gci (Windows)
-aoption allows you to see hidden files and directories (files and directories names that start with a period). In addition to the two files you created in step 2, you should see two hidden entries.and... These are relative names for the current directory and the parent directory.Make
git-laba repo by issuing the following command:git initThis will create a hidden subdirectory called
.git. This is the directory where all changes will be tracked for this repository. Do not modify or delete this directory or you will loose all your tracking information.Workflow
Locally, Git has three areas:
Workingdirectory (also calledIndex),Stagingarea, andRepository commit areaarea.-
Working directory (tree)
Any changes you make to your files (including adding new files) will be in this area. Newly created files will be considered
untracked. -
Staging area (index)
Files will be
trackedand changes will be ready to be committed. -
Repository Commit area (History)
Once files are committed, they become part of the repository history. They are tracked by the git system and a snapshot of your repository is created. Each snapshot is identified by a hash string.
Let's modify the first file. Normally in a repo we start by creating a
README.mdfile (created in step 2). Edit theREADME.mdfile and type your full name and your GitHub user name and save it.One of the common commands we use is to check the status of our repo is the
git statuscommand.Type the status command inside the
git-labdirectory and record the output of this command asAnswer 4.You now have an untracked file in your working area. Let's track the file by staging it. Issue the command:
git add README.mdCheck the status of your project and record the output of the command as your answer to
Answer 5.Did you notice the different file name colors in the two status command? Add the second file
answers.mdto the staging area.Check the status again and record your output as
Answer 6.Your files are now ready to be committed. Issue the commit command:
git commit -m "Initial commit"Messages in the commit command is very important. It describes what has happened since the last commit. It is specially helpful when working with teammates on the same project.
Issue the status command again and record your output as
Answer 7.The above workflow outlines the steps that you usually take to create a commit (snapshot) of your repository at any given time.
-
-
View the repository history (commits). Issue the
logcommand.git logThe output should include your commits' information including:
- Hash value (
0c6112a...). HEAD->master: a pointer to a repository calledmaster.masteris your main repositorybranchname. More on branching later.- Author, email, date, and comment made while committing.
Record the output of the
git logasAnswer 8. - Hash value (
Working with remote repositories on GitHub
-
Open a browser window and go to GitHub (
https://github.com), login if necessary.-
Create a new public repository (click the + symbol on the top right).
-
Name your repository
git-lab -
Do not select anything else.
-
Click on
Create repositorybutton. -
Now you have an empty repository called
git-lab. You should see three instructions on how topush an existing repository from the command line. These are the commands you need to push your local repository to GitHub. -
Let's try the commands (replace <user-name> with your github user name without <>):
git remote add origin https://github.com/<user-name>/git-lab.git git branch -M main git push -u origin main
or
git remote add origin git@github.com:<user-name>/git-lab.git git branch -M main git push -u origin main
These commands will also give your branch the name
mainWatch the output to make sure you did not get any errors. Your repo should be updated on GitHub. Refresh your browser to see your
README.mdandanswers.mdfiles on GitHub.Check the status of your repository. Record the output as
Answer 9. -
-
Update
README.mdlocally. Add your email address and a message informing your TA where you recorded your answers. At the terminal, addREADME.md,committhe changes, andpushyour changes to GitHub.git add README.md git commit -m "Enter a message here" git push
You may have to enter your GitHub credentials (token) here.
-
Open your browser and go to GitHub. Update
README.mdon GitHub by clicking onREADME.mdand then clicking on the edit button. Add your class information. For example,CS 2400, Section 100. ClickCommit changes, type the commit messageAdded class information, then clickCommit changes. -
Open a terminal window and look at
README.mdin your local directory. Were the changes you made online reflected in your local copy? Record your answer asAnswer 10. -
Try the push command again
git push. What happens? Record your answer asAnswer 11. -
Open the terminal window and
pullyour remote changes to your local repository. This is what you would normally do when the online version of your repo changes while you're working on your local one. Issue the command:git pull
Look at README.md in your local directory. Were the changes you made online reflected in your local copy? Record your answer to Answer 12.
Cloning a Repository
The git clone command downloads an existing repository to your local machine. This is something you will do often. You will be asked to clone a repository, work on it, and push your changes online. Let's try it.
-
Go online to GitHub
-
Create a new repository
- Name your repository
git-lab-2 - Select
Public - Check
Add a README - Click on
Add .gitignore: Nonebutton and selectC++ - Click
Create repositorybutton
- Name your repository
-
Click on the
<> Codebutton and copy the link -
Open a terminal window, if it's not opened already, and go to your 2400 directory and clone the repository. You will normally do this for most homework projects.
cd ~/2400 git clone <link you copied above>
-
This should download the repository directory to you machine. The directory should be called
git-lab-2. -
Go to the new repo's directory and issue the
ls -acommand. Record the output asAnswer 13. -
The file
.gitignorehas a list of files thatgitwill ignore when pushing the repo. For example, it ignores executable files.
-
-
(35 points) Lets create a C++ program in
git-lab2repository and update the online main branch.-
In your editor, create the file
git-lab-program.cc. -
Copy the following code into it
/* * File: git-lab-program.cc * Author: <Enter your name> * Date: <Enter today's date> * Description: Add Description */ #include <iostream> #include <iomanip> #include <cstdlib> using namespace std; int main(int argc, char const *argv[]) { /*add code*/ return 0; }// main
- Add your name and today's date in the comments
- Add the line
cout << "Hello Git!!" << endl;in the main function - Save your program
- Compile and run the program using the commands:
g++ -Wall git-lab-program.cc ./a.out
-
Did you get the right output? If not, fix your program and try again
-
Add, commit, and push your changes to GitHub (See Workflow above)
-
Go to your repository on GitHub
-
Are the changes you made reflected online? If not, make sure you did not miss any of the steps.
-
-
Last Steps:
- Go to the
git-labdirectory - Edit the file
README.mdand write the wordDonein the file - Make sure the files
README.mdandanswers.mdare saved - Add
README.mdto the staging area - Add
answers.mdto the staging area - Commit and push your changes to GitHub
I encourage you to learn more about Git. It is an essential tool for every computer science professional.
- Go to the