git is a distributed version control system which means multiple developers can work on a single project painlessly.
I am going to talk about git from scratch. So if you haven’t installed git command line tool, do install it from https://git-scm.com/ . Let’ start
git init
You can either clone an existing git repository or create a new one. We can use git init command to create an empty git repository.
Sohails-MBP-2:myRepo sohailaziz$ git init Initialized empty Git repository in /Users/sohailaziz/workspace/lazyHacker/HelloAndroid/myRepo/.git
This means now onward whatever you add or modify in this directory will be tracked by git. So let’s add some files
Sohails-MBP-2:myRepo sohailaziz$ echo "this is my first file">firstfile.txt Sohails-MBP-2:myRepo sohailaziz$ ls firstfile.txt
So I have added a new file with name firstfile.txt. Since git is tracking this repository we can check if something is added or changed using git status command.
git status
git status shows you the current status of the repository, as what branch we are on, what has been modified and whether it’s staged (ready to commit) or not.
Sohails-MBP-2:myRepo sohailaziz$ git status
On branch master
No commits yet
Untracked files:
(use "git add ..." to include in what will be committed)
firstfile.txt
nothing added to commit but untracked files present (use "git add" to track)
So we have firstfile.txt but it’s not yet been staged for commit. So every change we make we need to add that file for commit using git add command.
git add
git add adds the file/files to git repository and will track any future changes to that file/files. Let’s add firstfile.txt.
Sohails-MBP-2:myRepo sohailaziz$ git add firstfile.txt
Sohails-MBP-2:myRepo sohailaziz$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached ..." to unstage)
new file: firstfile.txt
Sohails-MBP-2:myRepo sohailaziz$
so now we can see using git status that firstfile.txt has been added. We can either remove this file from staging area or commit it. Let’s make our first commit using git commit .
git commit
git commit commits everything (all the files) in the staging area locally. Commits should be as small as possible and the commit message should be descriptive as what functionality has been added, removed or fixed. We use -m flag for commit message.
Sohails-MBP-2:myRepo sohailaziz$ git commit -m "adds firstfile.txt as an example" [master (root-commit) 776bb5a] adds firstfile.txt as an example 1 file changed, 1 insertion(+) create mode 100644 firstfile.txt Sohails-MBP-2:myRepo sohailaziz$
Congrats! we have made our first commit locally.
In the next post, I’ll be explaining how to see the the changes using git diff adding remote and pushing local changes to a github remote repository.
You can watch this how-to tutorial on lazyHacker youtube channel at https://youtu.be/I7YPfKQNr8k and the source code at aLazyHacker at github
Happy coding 🙂