Skip to content

Tanu-N-Prabhu/Git-Tutorial-for-Beginners

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

57 Commits
 
 
 
 

Repository files navigation

Git Tutorial for Beginners

Master Version Control

space-1.jpg
Image Credits - Jenkins Plugin

Table of contents

  1. Introduction

    1. Why Version Control System?
    2. Version Control Requirements
    3. Two Strategies of Version Control Systems
      1. Lock - Modify - Unlock Strategy
      2. Copy - Modify - Merge Strategy
    4. Version Control Systems Handling History
      1. Git
      2. Download
  2. Basic Operations

    1. Creating a GitHub Repository and Clone it
    2. Pull Data from the Remote
  3. Inside Git

    1. Git GUI
    2. Git Gitk
  4. Inside .git Folder

    1. Undoing Changes
    2. Git Reset
    3. Git Revert
    4. .gitignore
  5. Branches and Merges

    1. Fast Forward Merge
    2. Non Fast Forward Merge
    3. Conflict Resolving
    4. Git Rebase

Introduction

Why Version Control System?

Suppose you are working on a simple HTML project, suddenly your boss tells you to change the title of the project. Now you have another version, after that someone else comes and tells you to change the title. This keeps happening for a long time. Now in your system, you have several updated HTML files with new titles. This was the traditional way that the developer used to follow back in the day, where one cannot view what were the changes, and who changed them. This was a problem in those days, but with the help of version control systems, you can easily get the above information without having to save billions of different files in your system.

Version Control Requirements

  1. Backup and Restore
  2. Synchronization
  3. Undo
  4. Track Changes and Ownership
  5. Sandboxing
  6. Branching

Sandboxing - Place where you can put all your sample code you don’t need to be published, just like prototyping.

Two Strategies of Version Control Systems

  1. Lock - Modify - Unlock Strategy
  2. Copy - Modify - Merge Strategy

Lock - Modify - Unlock Strategy

space-1.jpg
Lock - Modify - Unlock Strategy

Copy - Modify - Merge Strategy

space-1.jpg
Copy - Modify - Merge Strategy

Here, Jyothi (developer 2) can resolve the conflict

  1. Remove her own changes and leave the last one.
  2. Take some part of her implementation and then push the latest version to the server.

Version Control Systems Handling History

space-1.jpg
Image Credits - Medium

Disadvantage if the entire history is saved in the main server, we need to create some backups

space-1.jpg
Image Credits - Medium

Each developer has a copy of their own repository, and he/she knows what was changed by who and what. If something happens, then we can use an individual repository as backup.

Git

One of the popular Version Control Systems. Please read the docs

space-1.jpg
Image Credits - Stack Overflow

Dowload here

After downloading you can check for the version in your command prompt

git --version

>> git version 2.30.0.windows.2


Basic Operations

Creating a GitHub Repository and Clone it

  1. Go to GitHub and create a new repository with a name Git-Tutorial-For-Beginners

    1. Make it Public
    2. Include a README.md file (enable this option).
  2. Clone the repository to your system. I personally use Visual Studio Code Terminal. You can use the evergreen Commmand Prompt Termial as well.

    1. Get the clone link from the dropdown which says (Clone or Download).
    2. Type git clone paste_the_link.
    3. Extra Material - How to clone a github repository
  3. To view the connections of your repository type in git remote -v.

  4. You can check the status of your repository using git status.

  5. You can add a new file using git add index.html. But the easy way to do it is to create a new html file in your Visual Studio code and then commit to your repository.

  6. To commit, use git commit -m “message” will only commit the changes locally.

  7. When you refresh your GitHub you won’t see any changes, because you need to push the changes to the remote server.

  8. To view the log use git log.

  9. To push use git push or sometimes you can use git push -u origin master to push the changes.

  10. Now when you refresh the GitHub repository you can view the changes.

Pull Data from the Remote

  1. Just make some changes in your initial file ( Main Repository)
  2. But in your local system these changes will not be reflected so you need to pull the change (repository) first. Make sure you do this when you are working on a larger project.
  3. To pull the change you can use git pull origin master.

Inside Git

Git GUI

If you are working on bigger projects then use Git GUI (Graphical User Interface).

  1. To start GUI use git gui&
  2. Edit something in your file and open GUI and hit Rescan to see the changes.
  3. To commit from the GUI, click on the file name from the unstaged changes folder, add a commit message and click on Commit.
  4. You need to Push to the main repository, choose master → origin to push the changes.
  5. Refresh the GitHub to see the changes.
  6. Extra Materials - Learn to Use the Built-in GUI Tool | Git

Git Gitk

  1. To use it type gitk.
  2. It is very obsolete. Mostly not used.
  3. Extra Materials - Learn to Use Gitk – the Built-in Repository Browser | Git

Inside .git Folder

space-1.jpg
.git Folder

The following steps are just to view the .git file hierarchy. Go play around and see what's in it.

  • To go into the git folder cd .git
  • To go into HEAD use cd HEAD
  • cat HEAD
  • cd refs
  • ls - l
  • cd heads
  • cat master (you will get a long integer which sorta looks like 867D3ER583FG89…)
  • Now when you open git log, the id of the commit matches the above.
  • Whatever you commit first it would be saved in the index folder.
  • cd objects/
  • ls - l
  • Blob (Binary large Object)
  • To view what's inside a Commit - git show -s --pretty=raw the (id of commit)
  • To go inside a tree git ls-tree (id)
  • To go inside a blob git show (blob id)
  • Inside the blob you can find the content of the file.
  • Incase of a simple blob 70e475949jfnu - The first two numbers (70) represent a folder and the rest is the file.
  • cd objects/
  • ls -l
  • cd 70
  • Inside that you will see the file e47---, you can find the contents in terms of binary file.

Undoing Changes

space-1.jpg
Undoing Changes

Git Reset

space-1.jpg
Git Reset

To viewback the commits use git reset

  • git reset --soft HEAD~1 (1 indicates how many commits back)

Git Revert

To revert back to the changes. Add git revert id (get the commit id)

.gitignore

space-1.jpg
.gitignore

Gitignore will be hidden. To hide any log files place it (commit )inside the gitignore so it will be hidden. And then you play with it.


Branches and Merges

In git everything is built using Graphs (nodes).

space-1.jpg
Branch and Merge
  • Open Git K using gitK to see all commits.
  • Each branch is a feature.
  • After finishing the feature we can finally merge them to the master.

Fast Forward Merge

space-1.jpg
Fast Forward Merge
  • A, B, C are commits.
  • HEAD will always point to the latest commit.
space-1.jpg
Fast Forward Merge

Creating a new branch

  • cd .git
  • ls - l
  • cat HEAD
    • ref: refs/heads/master
  • cd ..
  • To create a new branch call checkout → git checkout -b feature
  • cat Head
    • ref: refs/heads/feature
  • nano song.txt
    • Just add a new line or something.
  • git add .
  • git commit -m “first commit in feature”
  • nano song.txt
    • Just add another new line in the same file
  • git status - modified song.txt
  • git commit -m “second commit in feature”
  • git status - Nothing to commit
  • git log to see changes -- commit was second one ((HEAD → feature)
  • Let us switch to master
    • git checkout master
    • git log -- commit was another one (HEAD → master)
  • Merge the feature to the master branch
    • To merge you must always be in the master branch
    • git merge feature
      • Fast Forward
      • git log → (HEAD → master, feature)

Non Fast Forward Merge

About

Master Version Control

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published