Git is a software that allows you to keep track of changes made to a project over time. Git works by recording the changes you make to a project, storing those changes, then allowing you to reference them as needed.
GIT Project has three parts.
1. Working Directory : The directory where you will be doing all the work. Creating, editing, deleting and organizing files.
2. Staging Area : The place where you will list changes you make to the working directory.
3. Repository : A place where GIT permanently stores those changes as different versions of those projects.
Git workflow consists of editing files in the working directory, adding files to the staging area, and saving changes to a GIT repository. Saving changes to GIT repository called commit.
I. BASIC GIT Commands
- git init – Turns the current working directory into a GIT Project
- git status – Prints the current status of git working directory.
- git add <filename> – Adds the file into the Staging Area. [ After adding verify the same with git staus command ]
- git add <list of files> Add command also takes list of files.
- git diff <filename> – Displays the diff between the file in staging are and current working directory
- git commit -m “Commit Comment” – Permenently stores changes from staging area into GIT repository.
- git log – Prints the earlier versions of the project which are stored in chronological order.
II. Backtracking Changes
In GIT the commit you are currently on is known as the HEAD commit. In many cases, the most recently made commit is the HEAD commit.SHA – The git log command displays the commit log. The commit will contain SHA values for each commit. The SHA is the FIRST 7 Digit of the SHA.
- git show HEAD – Displays the HEAD commit
- git reset HEAD <filename> – Unstages file changes in the Staging area.
- git checkout HEAD <filename> – Discards the changes in the Working Directory.
- git reset <SHA> – It will reset back to the level of commit.
III. GIT BRANCHING
GIT allows us to create branches to experiment with versions of a project. Imagine you want to develop a new API on master branch, until you ready to merge that API in master branch, it will not available. So, in this scenario we create a branch and develop our new API and merge into master.
- git branch – Shows the current branches and current active branch you are in.
- git branch <new branch name > – Create a new branch
- git checkout <branchname> – Switches to the branch
- git merge <branchname> – This command is issued from master to merge branch into the master.
- git branch -d <branchname> – Delete the branch.
IV. GIT COLLOBORATION
Git offers a suit of colloboration tools to working with other’s project.
- git clone <remote_location> <clone_name> – Creates a new replica of git repository from remote repository
- git fetch – Update the clone. This will only update the existing files.
- git merge origin/master – Merge the local master with Origin Master.
- git push origin <branch> – Push your work to the origin.
V. Example Workflow to add a files
- git clone <remote_location>
- Eg. git clone http://www.abc.com/abc/bcd
- git add <new_files>
- git add abc.py bcd.py
- git status
- git commit -m”Commiting abc.py”
- git push origin master