How To Use Git Effectively [Part 1]
- This code was working yesterday but today it is not
- The code got deleted
- A weird bug has introduced suddenly and no one knows how?
If you have been in any of the above
situationsthen this post is only for you. This will help you to give you a workable understanding.
Question: What is git?
- First of all, Github is not git. Many people confused between them.
- Github is a website for hosting projects that use git using git basic commands.
- Git is a type of version control system(VCS) that makes it easier to keep track of changes to our files.
- For example, when you edit a file, git commands can help you determine exactly what changed, who changed it and why?
Question: Why Git?
The answer is very simple, Git allows teams to effectively and efficiently contribute code to the same project in an asynchronous way. This empowers teams to collaborate better and thus allow them to solve bigger and more complex problems.
Question: What does Git do?
Git tool does two main things:
- Track the history of your files.
- Share and collaborate on files.
Question: What is the operation
providedby git? Fig.1 :The Architecture of Git
[Note: Assumes that you have
downloadedand installed git on your system] Fig.2 :Git Basic Rule before you use
1. Configure Git :
After git is installed on your system, configure git so that you can understand who is pushing the code to the remote repository, as many people could be working on the same project.
$ git config --global user
$ git config --global user.email firstname.lastname@example.org
Git on your system use these credentials every time you push some code to the remote repository. You can also view all Git configuration
$ git config --list
2. Set Up and Initialization :
Check your git version with following command, which will also confirm that git is installed.
$ git –version
Initialize your current working directory as a Git repo
$ git init
To copy an existing remote git repository with repo’s URL or
$ git clone https://www.github.com/username/repo-name
Show your current git directory's remote repo
$ git remote
3. Staging :
When you have modified a file(s) and have marked it to go in your next commit, it is considered to be a staged file(s).
Check status of your git repo, including
both,staged and non-staged files.
$ git status
To stage a file that you modified
$ git add filename
For example: $ git add user.swift
To stage or add all files in the current directory including files that begin with a “.”
Remove a file from staging while retaining changed within your working directory
$ git reset filename
For example: $ git reset user.swift
4. Committing :
Once you have staged your all updates, ready to commit them, and record changes you have made to the repository.
To commit staged files, run commit command with your meaningful commit message so that you can track the commits later.
$ git commit –m “Your commit message”
Condense staging all tracked files and commit in one go
$ git commit –am “Your commit message”
Modify your commit message
$ git commit –amend –m “New commit message”
[To be continued…]