This will convert your current directory into a git repository
mkdir MyProject
cd MyProject
git init
Checking your status
git status
It shows the status of your current branch, the status of all the files in
your repository, and the status of the differences between your local branch
and your remote branch.
This is an example of a clean status. We will see more complicated status as
the tutorial proceeds.
$ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
Advice: always check your status before taking further actions.
Branch
git branch
git branch shows the status of the branches.
$ git branch
* master
This shows me that I have only one branch named "master". By default, "master"
is the main branch of a git repository.
Branch
git branch
We can create a new branch by type a new branch name after git branch:
$ git branch myNewBranch
Now check your branches status again.
$ git branch
* master
myNewBranch
We have two branches now, but our current working branch is still "master".
Branch
git checkout <branch>
We can switch to a different branch using the command git checkout
git checkout has many, many usages. We can use it to switch to a branch,
a commit, or a tag. We can also checkout a file or a directory. Even
more, we can combine the both. For example, checkout a file from a
specific branch. We will see some of these examples later.
Making changes
Let's start from creating a new file, name it "hello.txt", and add some
content to the file:
echo "Hello world" >> hello.txt
Now if we see our git status, we can see "hello.txt" is recognized as
an untracked file.
$ git status
On branch myNewBranch
Untracked files:
(use "git add <file>..." to include in what will be committed)
hello.txt
nothing added to commit but untracked files present (use "git add" to track)
Making changes
git add <path>
git add will convert our untracked file to a staged status.
$ git add hello.txt
$ git status
On branch myNewBranch
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: hello.txt
Making changes
git commit
We are not ready to commit our change. A new commit requires a commit message.
You may pass your commit message through the -m option.
git commit -m "Add the line Hello world to the file hello.txt"
Now check our status again. Everything is clean.
$ git status
On branch myNewBranch
nothing to commit, working tree clean
Checking the history
git log
$ git log
commit 4ce2556bc32186220054359bc275f8a6a90d4ca4 (HEAD -> myNewBranch)
Author: Henry Li <henry@clicktocloud.com>
Date: Fri Mar 6 11:20:14 2020 +1100
Add the line Hello world to the file hello.txt
Checking the history
git log
Many times, we can use git log -p instead, which will also show the
detailed changes.
$ git log -p
commit 4ce2556bc32186220054359bc275f8a6a90d4ca4 (HEAD -> myNewBranch)
Author: Henry Li <henry@clicktocloud.com>
Date: Fri Mar 6 11:20:14 2020 +1100
Add the line Hello world to the file hello.txt
diff --git a/hello.txt b/hello.txt
new file mode 100644
index 0000000..802992c
--- /dev/null
+++ b/hello.txt
@@ -0,0 +1 @@
+Hello world
Checking the changes
git diff
Using git diff can also check the changes. For example, git diff master
will show us the difference from the master branch to our current branch.
$ git diff master
diff --git a/hello.txt b/hello.txt
new file mode 100644
index 0000000..802992c
--- /dev/null+++ b/hello.txt
@@ -0,0 +1 @@
+Hello world
Making changes
Let's continue to make changes on the file "hello.txt".
echo "Hello again" >> hello.txt
Check the status. This time, the file "hello.txt" is in a modified status.
$ git st
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: hello.txt
no changes added to commit (use "git add" and/or "git commit -a")
Checking the changes
If there are files in a modified status, we can see the unstaged
changes using git diff.
$ git diff
diff --git a/hello.txt b/hello.txt
index 802992c..06ae335 100644
--- a/hello.txt+++ b/hello.txt
@@ -1 +1,2 @@
Hello world
+Hello again
File status
So far, we've seen all four different status of a file. They are:
Staged
Modified (unstaged)
Untracked (new)
Unmodified (clean)
File status
Discarding the changes
For different file status, commands for discarding the changes would be
different.
git checkout <path>
The most common situation is to discard modified/unstaged changes: we
need to use git checkout <path>.
git checkout hello.txt
Discarding the changes
git reset <path>
For files in staged status, we can use git reset <path> to convert it
to unstaged status, then use git checkout <path> to discard the changes.
git reset hello.txt
git checkout hello.txt
Discarding the changes
git clean -f <file>
For those untracked files, we may simply use rm <file> to get rid of it,
since git doesn't care about the file.
Alternatively, we can use git clean -f <file>.
touch randomNewFile
git clean -f randomNewFile
Pulling the changes
git pull
Git is a distributed version control system, which means there are also changes
made by other people from other places. We can get their update using the git pull command.
Usually we only care about the main branch (master) from our remote server.
So the following one would be a frequently used command:
git pull origin master
Pushing the changes
git push
When you think your changes are good enough, you'd probably want to push your
changes to the remote server. Usually, you wouldn't want to push the changes to
the main branch (master) directly, so we should do the push for our current
branch.
git push origin myNewBranch
When the push is finished, your local branch "myNewBranch" will be copied to
the remote repository.