What is Git?
Git is a version control system. Git is a opensource project originally developed by Linus Torvalds, the famous creator of the Linux OS.
Git is an example of a DVCS (Distributed Version Control System). Rather than have only one single place for the full version history of the software as is common in once-popular version control systems like CVS or Subversion (also known as SVN), in Git, every developer's working copy of the code is also a repository that can contain the full history of all changes.
Distributed (Git) | Centralized (CVS) |
Full local history | No |
Work offline | No |
Fast | Slow |
Rebase patches easily | Patches go stale |
Forks happen, deal with it. | Forks are painful. |
Powerful merging allows to use lots of branches. | Merging is painful |
Advantages of Git or DVSC.
DVCS clients not only check out the latest snapshot of the directory but they also fully mirror the repository. If the server goes down, then the repository from any client can be copied back to the server to restore it. Every checkout is a full backup of the repository. Git does not rely on the central server and that is why you can perform many operations when you are offline. You can commit changes, create branches, view logs, and perform other operations when you are offline. You require network connection only to publish your changes and take the latest changes.
Git is secure.
Git is secure against modifying the content without being able to know that something went wrong. Each commit references to the parents hash. Each item in the commit has its hash as reference and the commit itself is also hashed. So changing just one item would change the complete hash of the commit itself. So no one could easily change the content of a file in the git repository without an git integration error, even if that person has full access to the server.Git Cryptographic security
- Everything is check-summed before it is stored and is then referred to by that checksum.
- Checksumming using a SHA-1 hash.
- Is a 40-char string composed of hedadecimal characters (0-9 and a-f) and calculated base on the contents of a file or directory structure in Git.
- Something like this: Ib80d720bfdec1ffe100b1b6b5c752e72b583890b
- Git stores everything not by file name but in the Git database addressable by the hash value of its contents.
Git WorkFlow
As shown below,- The files are modified at the working directory.
- Git ADD is used to move the modified file to a staging area.
- Git Commit, will move the files from the staging area to Git Repo (local copy).
- Git PUSH, will move the changes permanently to the GIT repo.
Git Commands
git config: Configure the author name and email address to be used with your commitsgit init: create a new local repository.git clone: create a working copy of a local repository.git add: add one or more files to staging (index).git commit: Commit changes to local repo.git push: send changes to the master branch of your remote repository.git status: List the files you've changed and those you still need to add or commit.git remote add origin <server>: Connect your local repository to a remote server.git checkout: Switch from one branch to another.git branch: List all the branches in your repo.git pull: Fetch and merge changes on the remote server to your working directory.git merge: To merge a different branch into your active branch.git diff: view all the merge conflicts.
Diagram below summarizes various GIT commands,
Examples
git config:The first thing you should do when you install Git is to set your user name and email address. This is important because every Git commit uses this information.
$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com
Again, you need to do this only once if you pass the --global option.
git init, add and commit
Below image shows git commands, init add and commit in action.
At this stage we reached upto the local repo.
From here to proceed to remote repository we need git remote and push commands.
git remote and push
git remote will link the local repo to the remote repo.
git push will push the committed files from the local repo to the remote repo.
Check this youtube video to see the concepts we discussed so far,
Removing File from a Git Repository
git rm file1.txtBut if you want to remove the file only from the Git repository and not remove it from the filesystem, use:
git commit -m "remove file1.txt"
git rm --cached file1.txtAnd to push changes to remote repo
git commit -m "remove file1.txt"
git push origin branch_name
Deleting Git Repository
git remote -v: will list the repositories.
git remote rm <repo>: will delete the repository from local repo.
Git: Branch and Merge
Let's start creating a file and commit that to master branch.
As shown in above image, created a file called xyz.txt and committed to master branch.
As shown in above image, created a file called xyz.txt and committed to master branch.
The first commit we did with our file can be represented as below,
Git log will list the commit information.
git branch command, will create a new branch.
As noted below, the HEAD pointer move to master, john and bob branches.
The whole sequence of commands are shown below.
We use git log --decorate option to decorate the log.
git log --all --graph --decorate --oneline --simplify-by-decoration
git checkout, will move the head to branch john.
After checkout, on branch John the file get modified as shown below, and got committed to local repo.
The second commit will move the HEAD to the branch John. The whole sequence is captured below.
As shown below the HEAD move to the branch John.
Git: Merge Command
Merge will merge the commit 2 with the Master branch.
Below shows the sequence of commands to run.
Figure below shows the Fast-forward merge, as shown the HEAD moves to master.
As shown above and below sequence of commands.
The branch bob pointer, still remains at commit 1.
But on branch bob, file xyz.txt got updated. On branch bob the file now looks like as shown below.
The command sequence below shows the position of Master branch and branch Bob.
The branch bob pointer, still remains at commit 1.
But on branch bob, file xyz.txt got updated. On branch bob the file now looks like as shown below.
The command sequence below shows the position of Master branch and branch Bob.
We made an update on branch bob, we cannot merge that update to master with a fast-forward merge. Since the master has already advanced to the Commit 2 and Branch bob still remain at Commit 1.
Now commit the modification we made to file xyz.txt at branch bob.
The log will look like this,
Now commit the modification we made to file xyz.txt at branch bob.
The log will look like this,
Recursive Merge
Since there is no direct link between Master and Branch Bob. Git cannot do a Fast-Forward merge.
When we attempt a merge between branches master and bob. Git will perform a Recursive merge.
When we attempt a merge between branches master and bob. Git will perform a Recursive merge.
The sequence of commands are shown below, notice that git merge spit out a comment, "Merge made by the 'recursive' strategy'.". In place of 'Fast-Forward'.
Git: Merge Conflicts
Consider a scenario where Master branch and a child branch (John) has moved to same commit.
As shown below commit 3. But another branch called Bob is still in commit 1. Bob has made a modification to a file that Branch John has already modified.
At commit 2 and commit 3 the file, xyz.txt looked like this,
But Branch bob still struck at Commit 1.
Branch bob made a update to file xyz.txt it look like as shown below.
Now we have to merge xyz.txt from branch bob to branch master. A recursive merge could resolve this conflict.
But as shown below, the recursive merge failed. Branch Bob and Branch Master have changes on same line of code.
Git failed to resolve automatically. And asked user to resolve the conflict.
As shown below commit 3. But another branch called Bob is still in commit 1. Bob has made a modification to a file that Branch John has already modified.
At commit 2 and commit 3 the file, xyz.txt looked like this,
Branch bob made a update to file xyz.txt it look like as shown below.
Now we have to merge xyz.txt from branch bob to branch master. A recursive merge could resolve this conflict.
But as shown below, the recursive merge failed. Branch Bob and Branch Master have changes on same line of code.
Git failed to resolve automatically. And asked user to resolve the conflict.
As shown above auto merge failed. The merge command modified the file xyz.txt, with the conflict information. Now the user has to fix the issue and commit again, in other words user has to resolve the conflict.
A conflict resolved file may look like this,
A conflict resolved file may look like this,
No comments:
Post a Comment