Learning Git and GitHub:-
Git is a free and open source version control system, originally created by Linus Torvalds in 2005. Git is one of the popular decentralized Version control system(DVCS) . In this Version control system, dev team have their own code repository locally.
Git Support all functions like any other version control tools. Initial clone, merge, pull, push ,commit ,diff, branching, status check and commit history.
Below are high level overview of how Git works:
→ Create a Repository (project) with a git hosting tool. It can be Github/Bitbucket or any other tool.
→ Copy (or clone) the repository to your local machine
→ Add a file to your local Repository and commit (save) the changes made.
→ Then Push your changes to your master branch
** Make a change to your file with a git hosting tool and commit
→ Pull the changes to your local machine
→ Create a Branch (version), make a change, commit the change.
→ Open a pull request (propose changes to the master branch)
→ Merge your branch to the master branch.
Important Technical Terms and Terminology of Git:-
The Repository is Collection of files managed by git and all version (history)
Folder where we initialized git or content files in our storage known as Working Directory or workspace.
Commits : snapshot of file, create a timeline of changes in branch, git repository have at least one branch known as master branch.
GitHub: Git hosting/cloud service for unlimited public repository with no private repo but in business model, we can choose to have private repository also.
Working with Git:
Create repository to your local system.
In the beginning repository you create is going to be empty without any code in it. Gradually, will start adding some files to it soon.
This Git repository will be pushed to central repository (GitHub), which means that others can access that repository if you give them permission.
Set identity to git, that recorded in version log.
$ git config --global user.name "Khemnath Chauhan"
$ git config --global user.email “khemnath.devops@outlook.com”
Steps to Create Git Repo:-
git init → initialize the repository meaning it will create working area, staging area and commit area.
$ mkdir kncproject
$ cd kncproject
$ git init demo$ cd demo
$ ls -a (there folder .git, that is git repo)
Git States with Local:
Working Directory : All files present in above “demo” folder.
Staging Area : file preparing for commit, first store in staging area, then only we can commit
Repository/Commit History : Committed with version or save changes to git repo.
Connect git to remote repository:
Create a repository in GitHub.
$ git config --global user.name "Khemch"
# Run the command to connect to remote repository,
# it's called origin here can be anything that is easily understandable
$ git remote add origin https://github.com/Khemcha/java_project.git
# To check the remote repository
$ git remote -v
origin https://github.com/Khemcha/java_project.git (fetch)
origin https://github.com/Khemcha/java_project.git (push)
Git States with Remote:
Remote State : repo shared to centralized system like GITHUB
$ cat > test.txt First line
$git status
#Put file in the staging area, without putting file here, we can’t create commit history.
$git add test.html
#Send file to repo/commit area, here we have created snapshot of file means version of file
$git commit -m “first comment” text.txt
#To check all version of file, every commit they provide commit id
$ git log
OR,
$ git log --oneline --> To view in single short line.
Below is the sample output from git log command
#See details of log.
$git show commitID
#We can use commit id’s to see the diff in 2 version
$git diff id1 id2
#Remove git repo.
$rm -rf .git
# Again create git repo in existing project, go to project folder
$git init
# Add all file in staging area
$ git add .
# Add all file in commit area.
$git -m "comment" commit .
Create new file, that is not by default tracked by git
$ touch new.txt# Show tracked file by git .
$ git ls-files#Staging and Committing file in one command : Express commit$git commit -a -m “comment” file.txt Note: not all version of git support this
HEAD Markers : Special Markers Like pointers, points to last commit of current branch.
# How to make file untracked or removed from staging area:
$git reset HEAD new.txt
# Rollback file data to any point in time:
$git log file.txt
If you want to go back to specific timeline or previous commit content. Use below
~ Get commit id, at what point we want to rollback.
$git reset <commitID> file.txt sample from above log. To go to second line$ git reset c752bc5 file.txt$git checkout — file.txt
# Create a new branch:
Git branching allows developers to separate from the production version of code to fix a bug or add a feature. Developers create branches to work with a copy of the code without modifying the existing version. You create branches to isolate your code changes, which you test before merging to the main branch. Feature Branch is just a reference to master branch in storage (hard disk) they point to same file. Feature branch is not a complete backup copy by itself.
git branch <branch name>
Creating a branch this way does not automatically switch to the new branch. Git uses an asterisk and a different colored font to identify which branch is active. This designation represents the HEAD pointer showing which branch is active.
Alternatively we can can create using the git checkout as below.
$ git checkout -b <NewBranchNametocreate>
~ Below status shows the dev branch is ahead of master branch and master remote repository.