MY GIT NOTES…
I would like to start with a saying…
“a change is inevitable”
you’ll get to know why I mentioned it here very soon but let’s start with introduction…
What is Git?
Git is an open-source, distributed version control system developed by Linus Torvalds.
Created in 2005 to handle the increasingly difficult task of coordinating and consolidating the work of contributors to the Linux kernel.
It successfully handles it’s widely dispersed users…
Why do we need to have a version control system?
It’s for the software development of course.
In traditional software development approaches such as WaterFall Model — software features are planned in accordance with the software requirements, developed and improvements are released in one go…
But as I already mentioned the software requirements cannot be fixed because a requirement change is inevitable…
The integration of many small changes to the project leads to a continuously evolving project.
Therefore we go for modern software development practices, DevOps.
It involves continuously plan, build and release small improvements to your product.
And Git manages versions of your products forming a project history…
A version control system like Git helps Software developers to keep track of their project….
Git Overview
1.Git Locations:
a)Working Tree-A location in your computer that contains the directories and file of a single commit. Here you can view and edit the files of the project, preparing them for next commit.
b)Staging Area- Contains a list of files that are planned to be included in the next commit that you make.
c)Local Repository-Contains all the commits that have been made for the project. These commits represent the version history of the project.
d)Project Directory-The working tree, staging area and the local repository are commonly all contained in a single directory on a local computer, called Project Directory.
e)Remote Repository-Its a location in a Datacentre or Cloud. Contains all the commit of the project. If Local repository and Remote repository are synchronised then they have exactly the same commit.
2.GitFlow WorkFlow:
before that some important concepts…
Commit-It is a snapshot of the entire project at a given point in time. The collection of commits contain the history of the project. At any time you can review the project history and undo the changes by going back to the previous of the project.
Branch-set of commits that trace back to the project’s first commit. Branches are not aware of other branches.
Benefits of Branches:
- enable experimentation
- enable team development
- support multiple project versions
Merge-Involve merging of two branches.
Type:-
- Fast-Forward Merge
- Merge commit
- Squash Merge
- Rebase
Gitflow WorkFlow allows the safe continuous releases of the project. It enables a continuous train of project releases using multiple types of branches.
A typical Gitflow graph model…
3..Working with the Remote Repository:
Synchronize your local repository with the remote repository on, say Github.com(or any git hosting site).
- clone the remote repository on your local computer.
- commit changes to the local repository and push them to the remote repository.
- pull updates from your current local working branch with all new
commits from the corresponding remote branch on GitHub. - fetch all history from the remote-tracking branches
- merge the remote-tracking branch into the current local branch
4. Pull Requests:
A feature of git hosting sites. The ultimate goal of Pull requests is to merge a branch into the project’s master branch.
Two basic configurations:
- A single Remote Repository-Here a pull request is a request to merge a branch of the local repository.
- Multi Remote Repository-Here a pull request is a request to merge a branch from a Forked Repository into the Upstream Repository. Fork approach is common if the submitter doesn't have the write access to the Upstream Repository.
You can open a pull request, developers/authors discuss on the content if everything goes write your branch is merged…
Let's have some practice:-
My OS in Ubuntu 18.04 …
- Make a directory:
~$ mkdir hello-world
~$ cd hello-world
2. Making it a Git repository:
~/hello-world$ git init
The output will be:-
Initialized empty Git repository in /home/shivangi/hello-world/.git/
3. Now, let’s create a file:
~/hello-world$ touch fileA.txt
~/hello-world$ echo "Hello World"> fileA.txt
~/hello-world$ echo "I am using Git CLI">> fileA.txt
~/hello-world$ cat fileA.txt
The output will be:-
Hello World
I am using Git CLI
4. Now, checking Git status:
~/hello-world$ git status
The output will be:-
On branch masterNo commits yetUntracked files:
(use "git add <file>..." to include in what will be committed)
fileA.txt
nothing added to commit but untracked files present (use "git add" to track)rm
5. Adding the file to the staging area:
~/hello-world$ git add . //will add all your work to staging area
~/hello-world$ git status
The output will be:-
On branch masterNo commits yetChanges to be committed:
(use "git rm --cached <file>..." to unstage)
new file: fileA.txt
6.Git Commit:
~/hello-world$ git commit -m "First Commit"
Output:-
[master (root-commit) ea23785] First Commit
1 file changed, 2 insertions(+)
create mode 100644 fileA.txt
7. Git Status and Git log:
~/hello-world$ git status
On branch master
nothing to commit, working tree clean
~/hello-world$ git log --oneline
ea23785 (HEAD -> master) First Commit
Wow! Now you have made your first commit…
8.Making new branch and performing changes:
~/hello-world$ git checkout -b practice
Switched to a new branch 'practice'
~/hello-world$ echo "Performing changes from practice branch" >> fileA.txt
~/hello-world$ cat fileA.txt
Hello World
I am using Git CLI
Performing changes from practice branch
~/hello-world$ git status
On branch practice
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: fileA.txtno changes added to commit (use "git add" and/or "git commit -a")
~/hello-world$ git add .
~/hello-world$ git status
On branch practice
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: fileA.txt
~/hello-world$ git commit -m "Second commit"
[practice a465554] Second commit
1 file changed, 1 insertion(+)
Now, as you can see First commit is on the master branch so the commit success message tells us… and the Second commit is on Practice branch …
See the outputs of commit commands.
But, what exactly is the difference let’s see
9. Checking out to the master branch :
~/hello-world$ git checkout master
Switched to branch 'master'
~/hello-world$ cat fileA.txt
Hello World
I am using Git CLI
As you can see in the 8th point fileA.txt’s content is modified with an addition of a new line ” Performing changes from practice branch”…
But this line is not showing up when we open fileA.txt in the master branch (which is our default branch)…
Now, to show up the changes in the master branch we have to perform merging of practice branch & master branch…
Let’s see what happens
10. Git Merge:
~/hello-world$ git log --graph
* commit ea23785fa9e9f6220ec63f2a1b3c446755d6b9f6 (HEAD -> master)
Author: SHIVANGI JOSHI <shivangijoshi16399@gmail.com>
Date: Sun Nov 29 19:52:46 2020 +0530
First Commit
~/hello-world$ git branch -a
* master
practice //* shows we are at master branch
~/hello-world$ git merge practice
Updating ea23785..a465554
Fast-forward
fileA.txt | 1 +
1 file changed, 1 insertion(+)
//performed fast forward merge by default
~/hello-world$ git log --graph
* commit a4655540c140476cbe5008952c850330b9a8cf52 (HEAD -> master, practice)
| Author: SHIVANGI JOSHI <shivangijoshi16399@gmail.com>
| Date: Sun Nov 29 20:00:25 2020 +0530
|
| Second commit
|
* commit ea23785fa9e9f6220ec63f2a1b3c446755d6b9f6
Author: SHIVANGI JOSHI <shivangijoshi16399@gmail.com>
Date: Sun Nov 29 19:52:46 2020 +0530
First Commit
See, initially git log is showing only first commit, but after performing git merge it’s showing the Second commit too…
Now, Let’s have a look at FileA.txt
~/hello-world$ cat fileA.txt
Hello World
I am using Git CLI
Performing changes from practice branch
Wow!!! finally, our fileA.txt is updated. That’s the magic of Git until your changes are not ready to be updated, work on the practice branch and then just perform merge… but this is a very simple example in many cases one has to resolve merge conflicts(manually)…
But for now, I think I explained it well in case any doubt or any suggestion please leave a response…
Here’s a link to my GitHub repository for git commands…
In case you wanna contribute create a pull request :)
My notes are created when I was learning Version Control with git -by Atlassian…You can refer the course too.