0% found this document useful (0 votes)
13 views

week 12

The document discusses the importance of version control systems, particularly Git, for managing code changes and collaboration in software development. It outlines common problems faced when working alone or in teams, and explains how Git helps track changes, manage repositories, and facilitate collaboration. Additionally, it provides an overview of Git commands and workflows, as well as the distinction between Git and GitHub.

Uploaded by

bc210401244ama
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

week 12

The document discusses the importance of version control systems, particularly Git, for managing code changes and collaboration in software development. It outlines common problems faced when working alone or in teams, and explains how Git helps track changes, manage repositories, and facilitate collaboration. Additionally, it provides an overview of Git commands and workflows, as well as the distinction between Git and GitHub.

Uploaded by

bc210401244ama
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Version control with Git

week 12

1
Problems Working Alone
• Ever done one of the following?
 Had code that worked, made a bunch of changes and saved it, which
broke the code, and now you just want the working version back…
 Accidentally deleted a critical file, hundreds of lines of code gone…
 Somehow messed up the structure/contents of your code base, and
want to just “undo” the crazy action you just did
 Hard drive crash!!!! Everything’s gone, the day before deadline.

• Possible options:
 Save as (MyClass-v1.java)
• Ugh. Just ugh. And now a single line change results in duplicating the
entire file…

2
Problems Working in teams
 Whose computer stores the "official" copy of the project?
• Can we store the project files in a neutral "official" location?

 Will we be able to read/write each other's changes?


• Do we have the right file permissions?
• Lets just email changed files back and forth! Yay!

 What happens if we both try to edit the same file?


• Bill just overwrote a file I worked on for 6 hours!

 What happens if we make a mistake and corrupt an important file?


• Is there a way to keep backups of our project files?

 How do I know what code each teammate is working on?


3
Solution: Version Control
• version control system: Software that tracks and manages changes
to a set of files and resources.

• You use version control all the time


 Built into word processors/spreadsheets/presentation software
• The magical “undo” button takes you back to “the version before my last
action”

 Wiki’s
• Wiki’s are all about version control, managing updates, and allowing
rollbacks to previous versions

4
Software Version control
• Many version control systems are designed and used especially for
software engineering projects
 examples: CVS, Subversion (SVN), Git, Monotone, BitKeeper, Perforce

• helps teams to work together on code projects


 a shared copy of all code files that all users can access
 keeps current versions of all files, and backups of past versions
 can see what files others have modified and view the changes
 manages conflicts when multiple users modify the same file

 not particular to source code; can be used for papers, photos, etc.
• but often works best with plain text/code files

5
version Control
• Version Control

6
Source Code Management

7
Version control tools

8
What is Git?
• Git is a popular version control system. It was created by Linus Torvalds in 2005, and has been
maintained by Junio Hamano since then.
• It is used for:
 Tracking code changes
 Tracking who made changes
 Coding collaboration
• What does Git do?
 Manage projects with Repositories
 Clone a project to work on a local copy
 Control and track changes with Staging and Committing
 Branch and Merge to allow for work on different parts and versions of a
project
 Pull the latest version of the project to a local copy
 Push local updates to the main project

9
Working with Git
 Initialize Git on a folder, making it a Repository
 Git now creates a hidden folder to keep track of changes in that folder
 When a file is changed, added or deleted, it is considered modified
 You select the modified files you want to Stage
 The Staged files are Committed, which prompts Git to store a permanent
snapshot of the files
 Git allows you to see the full history of every commit.
 You can revert back to any previous commit.
 Git does not store a separate copy of every file in every commit, but keeps
track of changes made in each commit!

10
Why Git?

Over 70% of developers use Git!


Developers can work together from anywhere in the world.
Developers can see the full history of the project.
Developers can revert to earlier versions of a project.

Image from - dev2ops.org


11
What is GitHub?

Git is not the same as GitHub.


GitHub makes tools that use Git.
GitHub is the largest host of source code in the world, and has
been owned by Microsoft since 2018.

Git Install:

You can download Git for free from the following website:
https://www.git-scm.com/

12
Repositories
• Repository (aka “repo”): a location storing a copy of all files.
 you don't edit files directly in the repo;
 you edit a local working copy or “working tree”
 then you commit your edited files into the repo

• There may be only one repository that all users share (CVS,
Subversion)
• Or each user could also have their own copy of the repository (Git,
Mercurial)

• Files in your working directory must be added to the repo in order to


be tracked.

13
What to put in a Repo?
• Everything needed to create your project:
 Source code (Examples: .java, .c, .h, .cpp )
 Build files (Makefile, build.xml)
 Other resources needed to build your project: icons, text etc.

• Things generally NOT put in a repo (these can be easily re-created


and just take up space):
 Object files (.o)
 Executables (.exe)

14
Repository Location
• Can create the repository anywhere
 Can be on the same computer that you’re going to work on, which
might be ok for a personal project where you just want rollback
protection

• But, usually you want the repository to be robust:


 On a computer that’s up and running 24/7
• Everyone always has access to the project
 On a computer that has a redundant file system (ie RAID)
• No more worries about that hard disk crash wiping away your project!

• Options:
 attu, CSE GitLab, GitHub (do NOT use GitHub for homework!!!)

15
Aside: So what is GitHub?
• GitHub.com is a site for online storage of Git repositories.
• Many open source projects use it, such as the Linux kernel.
• You can get free space for open source projects or you can pay for
private projects.
• Do NOT use GitHub to store your homework!!
Question: Do I have to use GitHub to use Git?
Answer: No!
• you can use Git completely locally for your own purposes, or

16
Git Resources
• At the command line: (where <verb> = config, add, commit, etc.)
$ git help <verb>
$ git <verb> --help
$ man git-<verb>

• Free on-line book: https://git-scm.com/book/en/v2


• Git tutorial: http://schacon.github.com/git/gittutorial.html
• Reference page for Git: http://gitref.org/index.html
• Git website: http://git-scm.com/

• Git for Computer Scientists: http://eagain.net/articles/git-for-computer-scientists/

17
Centralized vs Distributed

18
GiT Features

19
Git file lifecycle

20
GiT Installation

21
Basic Workflow
Basic Git workflow:

1. Modify files in your working directory.


2. Stage files, adding snapshots of them to your staging area.
3. Do a commit, which takes the files as they are in the staging area
and stores that snapshot permanently to your Git directory (your
local copy of the repo).

• Notes:
 If a particular version of a file is in the git directory, it’s considered committed.
 If it’s modified but has been added to the staging area, it is staged.
 If it was changed since it was checked out but has not been staged, it is modified.

22
Get ready to use Git!
1. Set the name and email for Git to use when you commit:
$ git config --global user.name “Bugs Bunny”
$ git config --global user.email [email protected]

• You can call git config –-list to verify these are set.
• These will be set globally for all Git projects you work with.
• You can also set variables on a project-only basis by not using the
--global flag.
• You can also set the editor that is used for writing commit messages:
$ git config --global core.editor emacs (it is vim
by default)
• The latest version of git will also prompt you that push.default is
not set, you can make this warning go away with:
$ git config --global push.default simple 23
Create a local copy of a
2.
repo
Two common scenarios: (only do one of these)
a) To clone an already existing repo to your current directory:
$ git clone <url> [local dir name]
This will create a directory named local dir name, containing a
working copy of the files from the repo, and a .git directory
which you can ignore (used to hold the staging area and your actual repo)
Example: git clone
[email protected]:rea/superTest.git

b) To create a Git repo in your current directory:


$ git init
This will create a .git directory in your current directory which you
can ignore (used to hold the staging area and your actual repo).
Then you can commit files in your current directory into the repo:
$ git add file1.java
$ git commit –m “initial project version” 24
Git operations & Commands
• Download Git and Install
• Run Terminal
• GiT Bash Open (check Git Version using command git--version and
press enter)
• Create Sample Project Folder using mkdir command
 by default in C where windows resides
 Path (C\Users\User name\folder name)
• Create a file in which we have to work using “cd” command
• Add webpage using Notepad++ editor
• we can create multiple files on the same location
• Now, we use GiT for these two files

25
Git operations & Commands
• Now, we use GiT for these two
files “home.html and page.html”
using GiT Initiate
• GiT init command (GiT named
folder is created in working
directory)
• Staging Area command
 git add home.html
 after changes in html file
 run git diff command
• Now, we can track changes
26
Git operations & Commands
• “Git status” command will tell you the number of added
files in staging area
• How to push and pull files on Local and Remote
Directories from staging area?
 git commit-a-m (push all files to local repository and an attached
message what,where and when to commit changes in each file-
track changes)
 Then, push files to remote repository, for this sign up to GiT Hub
 Create Repository with “New” option. https://github.com/join
 New/name of repository/public or private/ Create (only one)
 Use URL of Repository to push files from LR to RR.

27
Git operations & Commands
• Add Repository in Git using command (can add multiple..)
 “git remote add <repository name> <repository url> ”
• example git remote add origin https://github.com....
• Now, push files from LR to RR, use the command
 “git push origin master”
 credentials for the first time
• Add a new branch using command
 git branch fix
 git branch command will show the total created branches
• fix and master

28
Branching

29
Branching
To create a branch called experimental:
• $ git branch experimental

To list all branches: (* shows which one you are currently on)
• $ git branch

To switch to the experimental branch:


• $ git checkout experimental

Later on, changes between the two branches differ, to merge changes
from experimental into the master:
• $ git checkout master
• $ git merge experimental

Note: git log --graph can be useful for showing branches.


Note: These branches are in your local repo!
30
Git commands
command description
git clone url [dir] copy a git repository so you can add to it
git add files adds file contents to the staging area
git commit records a snapshot of the staging area
git status view the status of your files in the working
directory and staging area
git diff shows diff of what is staged and what is
modified but unstaged
git help [command] get help info about a particular command
git pull fetch from a remote repo and try to merge
into the current branch
git push push your new branches and data to a
remote repository
others: init, reset, branch, checkout, merge, log, tag

31
After editing a file…
$emacs rea.txt
$ git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: rea.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
$ git status -s
M rea.txt  Note: M is in second column = “working tree”
$ git diff  Shows modifications that have not been staged.
diff --git a/rea.txt b/rea.txt
index 66b293d..90b65fd 100644
--- a/rea.txt
+++ b/rea.txt
@@ -1,2 +1,4 @@
Here is rea's file.
+
+One new line added.
$ git diff --cached  Shows nothing, no modifications have been staged yet.
$
32
After adding file to staging
area…
$ git add rea.txt
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: rea.txt
#
$ git status -s
M rea.txt  Note: M is in first column = “staging area”
$ git diff  Note: Shows nothing, no modifications that have not been staged.
$ git diff --cached  Note: Shows staged modifications.
diff --git a/rea.txt b/rea.txt
index 66b293d..90b65fd 100644
--- a/rea.txt
+++ b/rea.txt
@@ -1,2 +1,4 @@
Here is rea's file.
+
+One new line added.

33
Pulling and Pushing
Good practice:
1. Add and Commit your changes to your local repo
2. Pull from remote repo to get most recent changes (fix conflicts if
necessary, add and commit them to your local repo)
3. Push your changes to the remote repo
To fetch the most recent updates from the remote repo into your local
repo, and put them into your working directory:
$ git pull origin master
To push your changes from your local repo to the remote repo:
$ git push origin master
Notes: origin = an alias for the URL you cloned from
master = the remote branch you are pulling from/pushing to,
(the local branch you are pulling to/pushing from is your current branch)

34

You might also like