SAGARFIVE

1. Introduction

2. Git Basics

3.  Git Terminology

4.  Git Branching

5.  Git - Github

6.  Git Add (Git staging)

7.  Git Commit

8.  Github Advanced

9.  Git Remote

GIT

1. GIT : Git is Source code management tool and  an open-source distributed version control system to handle code among the developers for projects. Developed by Linus Torvalds in 2005 to develop linux kernel to handle project.

2. Version Control system :  is a software that tracks changes to a file or set of files over time so that you can recall specific versions later. It also allows you to work together with other developers. Uses of VCS are (i) complete history of file , (ii) simultaneously work facility, (iii) Branching and Merging, (iv) Traceability

3. Types of Version control system :  (i) Local , (ii) Central, (iii) Distributed

(i) Local version control system : it’s a user end specific – it’s a local database of tracking of changes to files in the code. Copy of code stored in a particular developer, so relation to other developers.

(ii) Central version control system : it’s a central server where code is stored, everyone can connect and work with that code and make changes , so it helps to handle work by many developers.

(iii) Distributed version control system : code stored at server with high availability and developers can copy that code to their local machine, when ever developers want ,they can send modified/developed code to server, and modified code can be updated to all developer local machines when they want to use the code.

4. GIT features : (i) Open source , (ii) Scalable, (iii) Distributed, (iv) Security(SHA1 encryption), (v) Speed, (vi) Branching and merging, (v) staging area ,  (vi) history

5. Benefits of GIT : (i) fast operation, (ii) offline working , (iii) undo changes – rollback, (iv) track changes

6. GitHub : GitHub hosts source code of your project in the form of different programming languages and keeps track of the various changes made by programmers and we can make the code public/private. We can integrate with git to handle your project. Here anyone from the world can contribute to that code.

7. SVN : Apache Subversion or SVN is centralized version control systems. Here you can checkout(get) single updated/last version of code from the central server where all the code is stored. You don’t have access to the entire project , you are specific to a particular version.

8. GIT vs. SVN : Git has complete project access but SVN no access, Git has SHA1 encryption but SVN doesn’t,

Git code distributed across all but SVN centrally stored.

9. GIT Install on Windows : (i) Goto URL : https://git-scm.com/downloads/win, select your version 32-bit or 64-bit , download it and install it. It’s done, git bash installed , you can open it by searching the name  on the windows machine.

10. Configuring GIT bash : open git bash , add your information to configure the git bash

(i) Add your username : git config  -–global user.name “username”

(ii) Add your email : git config –global user.email “your email id”

(iii) Add/change default editor to notepad/vim : git config –global core.editor vim or notepad

To Check the configured username, email and default editor, run this command git config –global –list

11. Workflow of GIT : Working directory ←→ Staging Area → Local Repository ←→ Remote Repository

10. Working Directory ( git init) : Where the source code is saved. We have to convert the folder into a working directory by running a git command .  git init  we run the git commands in git bash/terminal

Observations: .git folder created on working/project directory, and in git bash you can see name master.

11. Staging area ( git add):   It’s a place where git stores all changes.

(i) Untracked files : by default all the files in the folder/working directory are untracked files. Run the  command git status, the response

$ git statusOn branch masterNo commits yetUntracked files:  (use “git add <file>…” to include in what will be committed)        css/        font_awesome/        fonts/        index.html        js/nothing added to commit but untracked files present (use “git add” to track)

(ii) Tracked files : Run the  command git add . or specific file , to add them to track. After running the command, From that moment the files are under tracking. To check status run git status

$ git statusOn branch masterNo commits yetChanges to be committed:  (use “git rm –cached <file>…” to unstage)        new file:   css/bootstrap.min.css        new file:   font_awesome/font-awesome.min – Shortcut.lnk        new file:   font_awesome/font-awesome.min.css        new file:   fonts/PlusJakartaSans-Bold.ttf        new file:   fonts/PlusJakartaSans-Regular.ttf        new file:   fonts/PlusJakartaSans-SemiBold.ttf        new file:   fonts/PlusJakartaSans-SemiBoldItalic.ttf        new file:   index.html        new file:   js/bootstrap.bundle.min – Shortcut.lnk        new file:   js/bootstrap.bundle.min.js

Types of Tracked files : 

(i) Modified Files : after tracking , if you made some changes on any file, the git tracking system will identify it, mark it as a Modified file.

(ii) Un Modified : if you don’t change any code on any file , that file is marked as an unmodified file by git.

(iii) Remove files from Staging area(unstaging)/Tracking : if you developed a contact us page, at last you don’t want to add to         project repository, which is moved to server by committing and next to production, you can remove from the staging area, By running this command git rm –cached <file>…

12. Local Repository ( git commit) : it is snapshot/backup stage of code, if you done changes in your source code, then you can create commit with single line commit command : git commit -m “commit-name”,

git commit -m “project-01-user-commit-v1”[master (root-commit) 9705c5c] project-01-user1-commit-v1 10 files changed, 340 insertions(+) create mode 100644 css/bootstrap.min.css create mode 100644 font_awesome/font-awesome.min – Shortcut.lnk create mode 100644 font_awesome/font-awesome.min.css … create mode 100644 js/bootstrap.bundle.min.js

you can change source code, add another commit means moving code to local repository , then repeat the below commands 

git status → git add . → git commit -m “project-01-user1-commit-v2”

List of commits : git log – -oneline, then you can see the comments history/list

$ git log –oneline7902fbb (HEAD -> master) project-01-user1-commit-v29430e68 project-01-user1-commit-v1

To Revert back to Specific commit : command is  git reset – -hard <commit_id>

$ git reset –hard 9430e68HEAD is now at 9430e68 project-01-user1-commit-v1

You can see the list of commits : here we can see only one commit

$ git log –oneline9430e68 (HEAD -> master) project-01-user1-commit-v1

15. .gitignore : whenever programmers develop a project, rather than source code a lot of third party or development files like logs, class files , they don’t need in source code, here we can use .gitignore, create a file with .gitignore in working directory. add eliminated files like *.log or *.class , *.dev , save file , when you add git add . , it won’t add log, class, log

13.Remote Repository (git push) : storing the code on the internet based server, there are many clients github, gitlab, Bitbucket, Here choosing github, create github account , authenticate/integrate with git bash.

 (i).  Create a repository in github and copy url: https://github.com/sagar-gith/bootstrap-cards.git

(ii).  Rename branch to main (github default branch Main , but git branch is Master) : git branch -M main

        (iii). Add Remote repository :  git remote add origin <repository-url>

        (iv).  Push code to remote repository : git push -u origin main

Here it asks for authentication , log in with github or your remote clients, it’s done ! your code uploaded to remote repository

14. Git branch : This feature is provided in git, so that developers can create code related to different functionalities on separate branches. This helps the development team in creating the code in an uncluttered way. Later this code can be merged with the master branch. Master is final developed code that can move to testing , beta and production servers.

Simple project structure of Git types of branches : Feature branches, Develop, Release branches, Hotfixes, Master

Here we can see , programmers develop project features at feature branches,  if feature initial unit testing done, result is perfect than he merge that branch with Develop branch, next release , next hotfixes (production code bug fix),  next to master → testing server → beta server → Production server.

Creating new branchgit branch <branch-name>Ex: git branch feature-1
Rename branchgit branch -m <new-name>Ex: git branch -m “feature-one”
Move to a branchgit checkout <branch-name>Ex: git checkout feature-1
List the all branchesgit branch or git branch –l or git branch –list
Merge branch from existinggit merge <another-branch>Ex: git merge master
Delete a local branchgit branch -d <branch-name>Ex: git branch -d test-branch
Delete a Remote branchgit push origin -d <branch>Ex: git push origin -d test2
Commit a code at a stagegit commit -m “commit1 or any”
To see commit for branchgit log –oneline

16. git merge – branch : combining the branches commits on git. This works as a merging child branch → master/parent branch. All commits in the child branch are moved to the parent branch in a linear(time creation basis one after one) way.

Experience it with a task :

(i) Child branch commits : commit1, commit2, commit3, commit4, child branch implicitly grab the commits from Parent branch (master or main or any),

(ii) Parent branch commits : commit1, commit2, commit5

Here why we need child branch , which may be developed for specific feature, after testing it , developer needs that feature in main source code (master-branch) , so merge is a option to move child branch to master branch

Here the Procedure to merge ,

(i) first go to parent branch : git checkout master

(ii) merge the child branch :  git merge child

   Here it asks to create a commit, vi editor/default text editor opens, asks for commit, name commit save it, it done !

$ git log –oneline89ef490 (HEAD -> master) Merge branch ‘child to master with 2 commits’ca01d5c commit5aff6f99 (child) child-commit45bccd6b child1-commit34c57b8b commit2da8e071 commit1

Here you can see the all commits :  commit1, commit2, commit3,child1-commit3, child1-commit4, along with merge commit child to master with 2 commits.

**Observation: Merging won’t delete branch child, it only copies the commits and saves the merge history with a new commit as you can see above , it asks to save a commit while trying with merge commit.

17. git rebase : while developing a project , there are a lot of commits that may be in the main branch or child branch. We don’t know when the branch with commit is developed . If we perform a merge, it adds commits in time linear (time sort) basic, so it will effect in testing, moving to production that particular commit , so git rebase will merge child branch commits to main branch as latest commits.

Procedure to rebase :

(i) Move to child branch or any branch  : git checkout child

(ii) Rebase the master : git rebase master → command grab the master commit history , it will help full add child commits as latest , rebase won’t add commits to master branch , we need to run merge commands once again to add.

(iii) Merge child with master : git checkout master next,git merge child

$ git log –onelined6fb978 (HEAD -> master, child) commit7cf95f26 commit689ef490 Merge branch ‘child to master with 2 commits’ca01d5c commit5aff6f99 child-commit45bccd6b child1-commit34c57b8b commit2da8e071 commit1

** Observations :  commits are added on top of master branch as latest commits

18. Rearrange the commit order : you can change the order or commits, but the first commit is always fixed and you can’t modify it.

Command to re arrange : git rebase -i HEAD~4 ,
Then your git text editor opens you can change the order and save it, now you can check

Before rebaseAfter rebase
$ git log –oneline64e2112 (HEAD -> master) c56b5adbd c4f9b2718 c3e5b9a18 c20745895 c1$ git log –oneline3f254d5 (HEAD -> master) c2fb81faa c3c2678fd c4007ff98 c50745895 c1

19. git squash : when code developments many commits are raised, after releases we don’t need that many commits , then we can merge the previous commit.

Command to squash : git rebase -i HEAD~4

then interactive text editor opens , replace pick with squash to remove/merge the commit

File to edit with squashAfter squash
pick 7a8f6ec c6squash a585934 c4pick 0bae9c9 c3pick c7bee09 c2$ git log –oneline65bc9c6 (HEAD -> master) c267a817f c377d2817 c6007ff98 c50745895 c1

20. Cherry-pick : merging/moving particular commits to a specific branch.

Procedure :

         (i). Checkout to a particular branch :  git checkout main

        (ii). Command : git cherry-pick <commit-id> <commit-id>

        (iii)To get commit id by this command : git log –oneline

21. git stash (hiding) : stash is used for leaving the unfinished work, in such a way the git cannot access it and continue work on some other files, This can be done by git stash.

** To work on with you have do a first commit is mandatory

To stash(hide) the staged filesgit stash
To stash(hide) staged/untracked filegit stash -u
To see list of stashesgit stash list
To get back to stashed filegit stash pop
To bring the older stash outgit stash pop stash@{stash-number}
To drop last stashgit stash drop

1) To hide tracked/staged files :1) Do a first commit with f1,f22) add files f3 , f4 to the working tree using git add .3) git stash4) git statusstaged file hides$ git statusOn branch masterNo commits yetChanges to be committed:  (use “git rm –cached <file>…” to unstage)        new file:   f3        new file:   f4$ git stashSaved working directory and index state WIP on master: 7b00eca first-commit$ git statusOn branch masternothing to commit, working tree clean
2) To hide untracked/staged files :1) Do a first commit with f1,f22) create files f3 , f4 , f5, f63)  add f3,f4 to the working tree/staged using git add .4) f5,f6 untracked5) git stash -u4) git statusBoth staged/untracked file hides$ git statusOn branch masterChanges to be committed:        new file:   f3        new file:   f4Untracked files:  (use “git add <file>…” to include in what will be committed)        f5        f6$ git stash -uSaved working directory and index state WIP on master: 0150eba first commit$ git statusOn branch masternothing to commit, working tree clean
3) To see list of stash :git stash liststash@{0}: WIP on master: 0150eba first commit
4) Undo stashed files :git stash pop$ git stash popOn branch masterChanges to be committed:        new file:   f3        new file:   f4Untracked files:        f5        f6Dropped refs/stash@{0} (32f500a64d4f0d4eb94e01e334c341169788015e)
5) To see list of stash :git stash pop stash@<stash-number>touch f1 f2git add .git commit -m “fc”             touch f3 f4git add .git stashtouch f5 f6git stash -u$ git stash liststash@{0}: WIP on master: 5b4f3cd fcstash@{1}: WIP on master: 5b4f3cd fcgit stash pop stash@{0}Already up to date.On branch masterUntracked files:        f5        f6nothing added to commit but untracked files present (use “git add” to track)Dropped stash@{0} (ad8b357663b1ab95295ae22a680c339a9ec0e5eb)

22. Rollback to previous commit (git reset): if application has released version 6, it has huge bugs , then you can get back to version 5 by git reset using commit numbers.

command : git reset –hard <commit-id>

You can get commit id by this command : git log –oneline

23. git amend  : if you don’t want to add a new commit or there is no hude update to add commit , then you can do this update to the previous commit. If the previous commit is commit1, then you can add changes to this commit1.

command : git commit –amend -m “commit1”

You can get commit name by this command : git log –oneline

24. Git index (staging area) :  It’s a place where git stores all changes. it is used to build up a set of changes that you want to commit together.

25. git conflict : Conflicts generally arise when two people have changed the same lines in a file, or if one developer deleted a file while another developer was modifying it. In these cases, Git cannot automatically figure out which is correct. Hence, Git will notify the developer performing the merge that conflict is encountered.

perform the merge, to resolve the conflict.

26. git pull : to grab the code from remote repository branch specific

git remote add origin <repository-url>

git pull origin branch

26. git fetch : The “git fetch” command is used to pull the updates from remote-tracking branches, git pull = git fetch + git merge  

         git fetch <repository Url>  

git fetchgit pull
Fetch downloads only new data from a remote repository.Pull is used to update your current HEAD branch with the latest changes from the remote server.
Fetch is used to get a new view of all the things that happened in a remote repository.Pull downloads new data and directly integrates it into your current working copy files.
Fetch never manipulates or spoils data.Pull downloads the data and integrates it with the current working file.
It protects your code from merge conflict.In git pull, there are more chances to create the merge conflict.
It is better to use git fetch command with git merge command on a pulled repository.It is not an excellent choice to use git pull if you already pulled any repository.

GITHUB


1. Upload your local repository to remote/github repository:

1Add source code in a project foldercopy/paste code to a particular folder
2Open bash in that folder → convert folder into working directory, rungit init
3Move all files to staging areagit add .
4Add a commit to push code into local repositorygit commit -m “version-1”
5Create a repository in github and copy URL by login/signup at githubCopied URL : https://github.com/sagar-gith/bootstrap-cards.git
6Rename branch to main (github default branch Main , but git branch is Master)git branch -M main
7Add Remote repositorygit remote add origin <repository-url>
8Push code to remote repositorygit push -u origin main
Note : Here it asks for authentication , log in with github or your remote clients credentials,For github integration it may ask for a token which is used in place of password, generate a token at https://github.com/settings/tokens/new, then log in with github username and token.It’s done !  your code uploaded to remote repository

2. Clone/Copy the code from github repository :

1Copy the repository URL from github , https://github.com/sagar-gith/single-page-testimonial-webpage
2Create a folder for project and open git bash in that folder
3Clone the repository:
Command : git clone <remote-repository-url>It’s done ! Code downloaded in to the local repository/folder
$ git clone https://github.com/sagar-gith/single-page-testimonial-webpageCloning into ‘single-page-testimonial-webpage’…remote: Enumerating objects: 43, done.remote: Counting objects: 100% (43/43), done.remote: Compressing objects: 100% (34/34), done.remote: Total 43 (delta 6), reused 43 (delta 6), pack-reused 0Receiving objects: 100% (43/43), 4.32 MiB | 754.00 KiB/s, done.Resolving deltas: 100% (6/6), done.
4You can start working on project and git on it by moving into that projectcd single-page-testimonial-webpagegit init

3. Work on code and upload code to remote repository with new branch :

1Add remote repositorygit remote add origin <url>
2Git clone Master branch/any branchgit clone origin main or any branch
3Move inside foldercd <project folder>
4Initialize the git projectgit init
5Create a branchgit branch version2
6Checkout or move to new branchgit checkout version2
7modify/work on code
8Move the files to staging areagit add .
9Move the files to local repository by commitgit commit -m “verison2-commit1”
8Push the code to remote repositorygit push origin <branch>Ex: git push origin version2
It’s done ! go to github there you can see new branch created with your new code