what is git?
Git is a distributed version control system that tracks changes to files. It allows multiple users to collaborate and facilitates work in software development.
git repository
Git repository is a workspace that tracks and saves the history of all changes you made to the files.
-
Local Repository
- A repository located on a user's own machine.
- Remote Repository
- A repository placed on a dedicated server to be shared by multiple users.
check the git version
❯❯❯ git -v
git version 2.39.2
configuring your local git
❯❯❯ git config --global user.name "my name"
❯❯❯ git config --global user.email abcde@mail
You can verify your configurations
❯❯❯ git config user.name
my name
❯❯❯ git config user.email
abcde@mail
Initialize a new Git repository
When you run git init
, a directory called .git
will be created. This folder contains all the information necessary for Git to operate.
It's not recommended to initialize a new repository inside of an existing repository.
❯❯❯ git init
# or
❯❯❯ git init ${dirName}
❯❯❯ git init
Initialized empty Git repository in /Users/user/Documents/git_test/.git/
❯❯❯ ls -la
total 0
drwxr-xr-x 3 user staff 96 Sep 11 21:59 .
drwxr-xr-x 6 user staff 192 Sep 11 21:58 ..
drwxr-xr-x 9 user staff 288 Sep 11 21:59 .git
add and commit
git commit
is used to save changes to your code in a repository. Commit is like a save point in a video game. So you can return to the save point later like the players. You can create snapshot of your changes and commit messages which help you keep track of what changes you've made in each commit. It is recommended to write in present tense.
But before git commit
you have to stage your changes. When you run git add
, you are essentially telling Git which changes or files you want to include in the next commit.
I made two files which are test1.txt and test2.txt and run git add command.
# We have two files in this directory
❯❯❯ ls -la
-rw-rw-r-- 1 user staff 820 11 Sep 22:00 test1.txt
-rw-rw-r-- 1 user staff 200 11 Sep 22:00 test2.txt
# Add all files in this directory
❯❯❯ git add .
# If you want to add specific files, you just need to specify the files
❯❯❯ git add ${file1} ${file2}
# Now we added two files to staging area
# You can check the status by running git status command
❯❯❯ git status
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: test1.txt
new file: test2.txt
# The -m flag allows you to pass in an inline commit message
❯❯❯ git commit -m "initial commit"
[main (root-commit) 36802f1] initial commit
2 files changed, 2 insertions(+)
create mode 100644 test1.txt
create mode 100644 test2.txt
# You can check all commits you have made by running git log command
❯❯❯ git log
commit 3680...5746 (HEAD -> main)
Author: me <me@mail.com>
Date: Mon Sep 11 22:09:01 2023 -0700
initial commit
lines 1-5/5 (END)...skipping...
commit 3680...5746 (HEAD -> main)
Author: me <me@mail.com>
Date: Mon Sep 11 22:09:01 2023 -0700
initial commit
~
diff
The git diff
command allows you to see
- differences between working directory and stage
- differences between stage and repository
- differences with specific files
# 1. diffs between working directory and stage
❯❯❯ git diff
# 2. diffs between stage and repository
❯❯❯ git diff --staged
# 3. diffs with specific files
❯❯❯ git diff test2.txt
diff --git a/test2.txt b/test2.txt
index d606037..3b08777 100644
--- a/test2.txt
+++ b/test2.txt
@@ -1 +1,3 @@
-test2
\ No newline at end of file
+test2
+addline
+addline2
Top comments (0)