Git Lec1
Git Lec1
About Git
• Created by Linus Torvalds, creator of Linux, in 2005
• Came out of Linux development community
• Designed to do version control on Linux kernel
• Goals of Git:
• Speed
• Support for non-linear development (thousands of parallel branches)
• Fully distributed
• Able to handle large projects efficiently
Centralized VCS
• In Subversion, CVS, Perforce, etc. A central
server repository (repo) holds the "official
copy" of the code
• the server maintains the sole version history of
the repo
• You make "checkouts" of it to your local copy
• you make local modifications – your changes are
not versioned
• When you're done, you "check in" back to
the server
• your checkin increments the repo's version
Distributed VCS (Git)
• In git, mercurial, etc., you don't "checkout" from
a central repo
• you "clone" it and "pull" changes from it
• Your local repo is a complete copy of everything
on the remote server
• yours is "just as good" as theirs
• Many operations are local:
• check in/out from local repo
• commit changes to local repo
• local repo keeps version history
• When you're ready, you can "push" changes
back to server
Advantages of Git
• Working Directory : The working directory is the place where files are checked out.
• Staging Area or Index : Whenever you do commit an operation, Git looks for the
files present in the staging area. Only those files present in the staging area are
considered for commit and not all the modified files.
• Blobs : Blob stands for Binary Large Object. Each version of a file is represented by
blob.
• Tags : Tag assigns a meaningful name with a specific version in the repository.
• Pull : Pull operation copies the changes from a remote repository instance to a local one.
• Push : Push operation copies changes from a local repository instance to a remote one.
• HEAD : HEAD is a pointer, which always points to the latest commit in the branch.
• Git provides the git config tool, which allows you to set configuration
variables.
• Git stores all global configurations in .gitconfig file, which is located in
your home directory.
• To set these configuration values as global, add the --global option,
and if you omit --global option, then your configurations are specific
for the current Git repository.
Customize Git Environment
• Setting username:
• git config --global user.name “Alpha Beta“
• Setting email id
• git config --global user.email “[email protected]"
• These information is used by Git for each commit.
• Apart from these, you can have color highlighting for Git console,
choose default editor for Git and set default merge tool.
• If necessary, you also update the working copy by taking other developer's changes.
• You commit changes. If everything is fine, then you push the changes to the repository.
• After committing, if you realize something is wrong, then you correct the last commit and
push the changes to the repository.
Git Work-Flow
Git Work-Flow
• Modify files in your working directory.
• Stage files, adding snapshots of them to your staging area.
• Commit, which takes the files in the staging area and stores that snapshot
permanently to your Git directory.
Local Git areas
• When you said git init in your project directory, or when you
cloned an existing project, you created a repository
• 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
Making commits
• 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
• git add newFile1 newFolder1 newFolder2 newFile2
• Committing makes a “snapshot” of everything being tracked into your repository
• A message telling what you have done is required
• git commit –m “Uncrevulated the conundrum bar”
• git commit
• This version opens an editor for you the enter the message
• To finish, save and quit the editor
• Format of the commit message
• One line containing the complete summary
• If more than one line, the second line must be blank
Commits and graphs
• 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
• 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
• Hence, most commit objects have a single parent
• You can also merge two commit objects to form a new one
• The new commit object has two parents
• Hence, commit objects form a directed graph
• Git is all about using and manipulating this graph
Working with your own repository
• You can also take any previous commit object, make changes to it, and commit
those changes
• This creates a branch in the graph of commit objects
• You can merge any previous commit objects
• This joins branches in the commit graph
Commit messages
• All repositories are equal, but it is convenient to have one central repository in
the cloud
• Here’s what you normally do:
• 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:
• It is your responsibility to merge your two versions
• This is a strong incentive to commit and upload often!
• Git can often do this for you, if there aren’t incompatible changes
Typical workflow
Second commit
Third commit
Bob gets a copy
Fourth commit
Bob’s commit
Merge
Keeping it simple
• If you:
• Make sure you are current with the central repository
• Make some improvements to your code
• Update the central repository before anyone else does
• Then you don’t have to worry about resolving conflicts or working with multiple
branches
• All the complexity in git comes from dealing with these
• Therefore:
• Make sure you are up-to-date before starting to work
• Commit and update the central repository frequently