Git for beginners- 5 mins
What is Git ?
Git is a source code management system. To put it simply, there comes a need for management when there are more than one (even 100) of contributors writing code/contributing patches to a project. This where git kicks in and does the magic.
Git also provides you a mechanism of saving the “state” of the project. Saving the “state” means that you can take a snapshot of the project once you are done with your changes to related files. You can have a series of “snapshots” of the project which will enable you to keep different versions of the project without introducing any kind of redundancy i.e. you will have one copy of the project but still you can move back and forth between the states of the project.
You’ll probably understand git better once you start using it. Let’s get started.
Well, I will assume that you are a programmer and have some familiarity to linux environment command line.
On Ubuntu/Debian based systems:
On Fedora/RHEL based systems:
Setting up git
Initialize a git directory
We will first initialize a git directory. Initializing a git directory means telling the git that start tracking the changes in the directory.
Create a demo file
Go ahead and create a ‘demo.txt’ file in the ‘test’ directory we created above. This is just a example file for understanding how git works. After creating the file, enter the following line:
Now go to the terminal and query the status of the directory. To do that:
You will see the following output:
Congratulations ! You have learned your first git command. ‘demo.txt’ is listed in ‘Untracked files:’ because git is aware that a new file has to added to its directory. Isn’t that magic ? (You’ll see more magic by git later)
Go ahead and tell git that start tracking the file “demo.txt”.
Now, git will not only track each and every activity in the ‘test’ directory but also on the file demo.txt you just added.
You can again query the status of directory (git status) and see the output yourself. :)
First Snapshot (Commit)
Now let’s take the first snapshot of the directory. In technical terms, the snapshot is known as ‘commit’. It will save the state of our ‘test’ directory. For understanding purposes, I will go ahead and describe the state of our directory here:
This describes the basic snapshot of the directory.
Saving the snapshot (git commit)
Congratulations ! You have made your first commit. You can run ‘git log’ to see your commits.
Let’s recap what we done till now:
- Initialized a git directory
- Created and edited demo.txt
- Added demo.txt to git for tracking (git add demo.txt)
- Initial commit (git commit -m “commit message: Initial commit”)
You’ll see the real magic of git now. Go ahead and edit demo.txt. I will append the following line in demo.txt
Again query the status of directory : git status
To see what has changed since last commit (i.e. since last snapshot):
If you want to save the latest snapshot (see above code block), go ahead with git add and git commit.
Therefore, play with the file in the directory. Add some more files, edit them and see the differences with “git diff” command. You’ll understand why git is magical ;-)
Common workflow for git
The common workflow you’ll mostly use :
- Edit the files in the git directory
- git diff (to see the edits you made to directory)
- git add file1 file2 …
- git commit -m “commit_message”
- git log (to see all your commits)
The output of git diff here will be probably very small here as you are just using them from learning purposes. Here is a look how “git diff” makes life much more easier when you will be editing code of some project and see the edits you made.
The term “patch” is more or less the diff only.
Here is an example of a patch.
So this is what you are going to do 90% of the time while playing with source code of any projects. I would like to mention here that this is only the tip of the iceberg. There are lot more advanced things you can do with git which can help you become better programmer. :-)
Stay tuned. Happy hacking!