Learning version control using Git

Daily Tasks as Programmer

  • Create “Things”
  • Save “Things”
  • Edit “Things”
  • Save “Things” AGAIN!

Naive method have version control

Problems in naive method

  • No one knows who changed what
  • We created duplicate copies of same thing (wasting space)
  • No one took time to note what was exactly changed

What we wanted to do? - SAVE THINGS AGAIN

  • Who did it?
  • Why they did it?
  • What exactly was changed?
  • We want to keep track of history

Version Control comes to rescue

  • Method to track changes to file(s) and folder(s) and your binaries.
  • Who changed what, why and when? - via commit messages.
  • When did they change it - saving time and date with every commit.
  • Identifying each change seperately with a use of a unique identifier - SHA1 hashes.
  • Non-Distributed [Centralized] – Subversion, CVS
  • All commit goes to centralized master repo
  • Distributed – Git, Mercurial
  • Commit goes to remote repo but everyone has a copy (clone)

Why Git?

  • It's free and open-source.
  • You can use git even when you're offline
  • Code revisions (commit) that you made would be available to only you locally unless you push to remote repo.
  • Create and support parallel branches, work locally, isolate your work and merge seamlessly to collaborate.
  • It takes snapshots of changes and treat files as stream of snapshots over time.

  • It compresses the .git contents
  • It has integrity
  • The mechanism that Git uses for this checksumming is called a SHA-1 hash. This is a 40-character string composed of hexadecimal characters (0–9 and a–f) and calculated based on the contents of a file or directory structure in Git. A SHA-1 hash looks something like this:
    24b9da6552252987aa493b52f8696cd6d3b00373

  • Git stores everything in its database not by file name but by the hash value of its contents. These hash values are used very frequently.

Demo

  • Configure git
  • Forking Repositories on Github
  • Cloning Them
  • Making Changes
  • Commiting those Changes
  • Adding Remotes
  • Checkout
  • Pushing Your Changes
  • Branching
  • Making Pull Requests
  • Pulling New Changes and more ..

Git Workflow

Configuring git
git config --global user.name "John Doe"
 git config --global user.email "johndoe@example.com"
 git config --global core.editor "nano"

  • This is done for git to identify who is making the commits. For newbies, we recommend going with nano at the beginning as the editor of choice.

Obtain Repository
[Make your own/Clone from upstream]
git init
git clone {repo_url} {directory_name} 
Stage Changes
[Untracked Changes --> Staging them]
git add {filename}
git status

  • Git tracks everything in your directory. Go ahead make and edit some new files and let git do all the heavy lifting for you.

Commit Changes
[Staged changes --> Commiting them]
git commit -m “Commit message”
git status
git log
Defining Remotes
git remote -v
git remote add {remote_name} {repo_url}

  • A remote is basically a bookmark that leads to a different repository from which you may wish to pull or push your code.

Push Changes to Remote Repo
git push {remote_name} {branch_name}

  • Pushing to a repository requires authentication that needs to be provided in the form of your Github username and password.

Repeat till you get them right ...
Then move on to more advanced topics such as branching, rebasing and cherry-picking commits.

A bit about git internals

  • Commits are actually SHA1 hashes that creates the distinction
  • All commits in repo are nodes of a tree
  • .git folder contains:
    • config – Configuration files
    • logs/* - All the logs for commits
    • objects/* - The object repository
    • refs/remotes/* - tracking other remotes
    • Index – index cache (staging area)
    • HEAD – points to the latest commit on current branch

Where to go from here?

and more...

  • Implement git and GitHub in your future projects.
  • Contribute to major upstream open-source projects like Mozilla, Django, Linux kernel and many more.
  • Sharpen your skills and learn new technologies by solving issues.
  • Widen your network and find fun new side-projects.
  • Use your GitHub profile to showcase projects on your resume.

Resources & References

That moment when you realise the power of Git

Contributors

If you like what you saw, then do thank the contributors :-

.. or if you didn't like it, then use your newfound knowledge of git to open an issue and contribute here on GitHub.

THE END

Questions?


Shout out on Twitter: @ErSanyamKhurana
Shoot a mail at: sanyam@sanyamkhurana.com