Git Introduction
What is Git?
Git is a distributed version control system. It helps to track changes in files, typically source code. It allows to collaborate with group of people without overwriting eachother's work.
Git Installation
On macOS
Git comes bundled with the Command Line Tools for Xcode. To check if it’s installed, open Terminal and type:
git --version
or
git -v
Setting Personal Info to Git
Git requires you to set your name and email before you start committing changes. This information is stored in your commits.
git config --global user.name "yourusername"
git config --global user.email "youremail@example.com"
Basic Commands
follow along step by step:
- Create a folder/ directory called
git_module
mkdir git_module
cd git_module
- Create a subfolder called
first_app
mkdir first_app
cd first_app
- list files in the directory
ls -l
- Create a new file in the directory.
nano file1.txt
-
In the text editor, write some texts 7.
Ctrl+X
and press Y -
Initialize your git repository
git init
By this step, you have succesfully initialized a git repo
Spoonfeed git changes
Git doesn’t automatically track changes in your files. You need to tell Git when a file is added, modified, or deleted.
Files that Git doesn’t know about are called untracked files.
Untracked files aren’t included in commits, and Git won’t keep a history of them until you explicitly add them.
If an untracked file is deleted before being added, Git cannot recover it.
Tip: Always use git status to see which files are untracked, modified, or staged for commit.
Untracked files are in red:
Adding git files
This is adding untracked files.
git add filename.txt
Here, the files are transformed from unstaging areas to staging area.
If you have mutilple untracked files, then you can track it all at once.
git add .
Git Status
To check the status of git:
git status
Files that Git already knows about. If changes are staged for commit, they appear in green.. New files Git doesn’t know about yet. These appear in red.
commit changes
Commits the staged changes to the repository. It basically snapshots your repo at a specific point. In other words, it is recording the changes in the repo.
git commit
This opens a text editor to type your commit.
- Press I to enable Insert.
- Type your commit message.
- Press Escape key.
- type
:wq
and press Enter.
If you are smart then just do: (Quick Commit)
git commit -m "your commit message"
commit history
View your commit history.
git log
This will display commit hash, Author, Date and Commit message.
Summary
-
Create a local repo:
git init
-
When we create a new file in the repo, git is unaware of it.
git add filename.extension
- Now git is in staging area and git is aware of the file.
git commit -m "commit message"
git status
git log