git --version
A version controller
you can make great products from an application by only using 20% of its functions.
You only need to know the basics
STAGES
of gitSTAGES
of gitRepository
which you can see as a project file (often a directory)
What is the stage that you write your codes?
mkdir gitPractice # Make an new directory
cd gitPractice # Go to the directory
git init
Initialized empty Git repository in $(pwd)/.git #you can do "echo $(pwd)" to verify if the command is correct
echo "Never Care U" > NCU # This will create a file called `NCU` with content "Never Care U"
After finishing a period, what stage will you want to enter
OFTEN USED COMMAND
git status
branch
are you onOFTEN USED COMMAND
git add .
.
stands for everythinggit add NCU
OFTEN USED COMMAND
git status
After staging a file, what will you want to do?
Which is commit
.
This means to put all the thing from stage into your repo, and telling what have you done or why do you do this.
We need a GOOD convention of commit so that we know what you are writing
<type>[optional scope]: <description>
[optional body]
[optional footer(s)]
type
-> need to discuss before using, however it often contians and can be more of them
FIX
ADD
FEAT
DOCS
TEST
<type>[optional scope]: <description>
[optional body]
[optional footer(s)]
scope
-> sometimes we don't need them, but when we are in a big project, it will give a better view of what the error isdescription
-> the reason you make this commitbody
-> further descriptions if neededfooter
-> ReferencesTrust me, you will find out that this is hard.
OFTEN USED COMMAND
git commit
[master (root-commit) 6d13e9f] ADD: add NCU for demostrating how to commit.
1 file changed, 1 insertion(+)
create mode 100644 NCU
OFTEN USED COMMAND
git status
On branch master
nothing to commit, working tree clean
Go ahead and do something else to your file
echo "Nobody Cares U" > NCU # This will overwrite `NCU` with content "Nobody Cares U"
OFTEN USED COMMAND
git status
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: NCU
no changes added to commit (use "git add" and/or "git commit -a")
git commit -m "FIX: typo of NCU"
How to see the changes and what happens is the most important thing.
git log
-> repo's historygit reflog
-> whole git historygit log
commit 9b605fe216228416f4b999f34c17d05f33f33ef1
Author: N0Ball <cwj0325@g.ncu.edu.tw>
Date: Tue May 17 20:32:10 2022 +0000
FIX: typo of NCU
commit 6d13e9f8e8deab9f1724a63e6ce1845548207a75
Author: N0Ball <cwj0325@g.ncu.edu.tw>
Date: Tue May 17 20:23:41 2022 +0000
ADD: add NCU for demostrating how to commit.
It's simply just a nonsense file with nonsense words; if it is for
teaching it can be anything else. I suggest it to be a meme.
git log --oneline
9b605fe FIX: typo of NCU
6d13e9f ADD: add NCU for demostrating how to commit.
You will never ever put an unworkable program on master
git config user.name "jason"
git config user.email "jason@g.ncu.edu.tw"
OFTEN USED COMMAND
git branch jason
git branch dev
jason
-> your branch namedev
-> short of developeALWAYS REMEMBER TO CHECK WHICH BRANCH YOU ARE ON
OFTEN USED COMMAND
git status
List all the branches you have (LOCAL)
OFTEN USED COMMAND
git branch
dev
jason
* master
You can see that we have three branches.
Switch through branches
OFTEN USED COMMAND
git checkout jason
jason
-> the target branch you want to switch toSwitched to branch 'jason'
Remember to check if it is successed by status
Make more changes!
echo "Nation Central University\!\!\!\!" >> NCU && echo "BAD" > AUTHOR # This appends "Nation Central University!!!!" to the file NCU and creates a file `AUTHOR` with content "BAD"
Append words to NCU is important for future usage.
OFTEN USED COMMAND
git status
Have U done it right?
How to check your git movements?
git reflog
b50471b HEAD@{0}: commit: ADD: add comments to author
9b605fe HEAD@{1}: checkout: moving from master to jason
9b605fe HEAD@{2}: commit: FIX: typo of NCU
6d13e9f HEAD@{3}: commit (initial): ADD: add NCU for demostrating how to commit.
dev
branch.master
branch.jason
branch.1.
once again.git config user.name
git config user.email
git log --graph --all
graph
-> The graph viewall
-> not only the current branchnetwork
merge
) them together!Making two branches together.
you have to be on master
branch!
OFTEN USED COMMAND
git merge jason
jason
-> your targeted branchbranch jason
will not change, that's reasonable, since you can only modify what's on your current branch.
git log --graph --all
git merge dev
Let's merge dev and see what happens!
* N0Ball -> Merge branch 'dev' (HEAD -> master)
|\
| * N0Ball -> FIX: typo of filename (dev)
| * N0Ball -> ADD: add readme file
* | N0Ball -> Merge branch 'jason'
|\ \
| * | jason -> FIX: modify author's comment to make it more readable (jason)
| * | jason -> ADD: add comments to author
| |/
* | N0Ball -> ADD: add something more to make the graph better
* | N0Ball -> ADD: add something to make the log graph looks cool
|/
* N0Ball -> FIX: typo of JASON
* N0Ball -> ADD: add JASON for demostrating how to commit.
dev
branchNCU
jason
had append words to NCU
master
-> NCU
had appended wordsdev
and master
have its own appended words -> git doesn't know which to use -> CONFLICTOFTEN USED COMMAND
git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: JASON
no changes added to commit (use "git add" and/or "git commit -a")
FOREST SMELLS GOOD <<<<<<< HEAD National Central University!!!! ======= Very long president duration >>>>>>> dev
<<<<<< HEAD
-> current status>>>>>> dev
-> incomming changesYou should choose. Keep both, leave one, delete both… whatever you like!
* N0Ball -> Merge branch 'dev' ( (HEAD -> master))
|\
| * N0Ball -> ADD: add some words to JASON to make merge conflicts ( (dev))
* | N0Ball -> Merge branch 'dev' ()
|\|
| * N0Ball -> FIX: typo of filename ()
| * N0Ball -> ADD: add readme file ()
* | N0Ball -> Merge branch 'jason' ()
|\ \
| * | jason -> FIX: modify author's comment to make it more readable ( (jason))
| * | jason -> ADD: add comments to author ()
| |/
* | N0Ball -> ADD: add something more to make the graph better ()
* | N0Ball -> ADD: add something to make the log graph looks cool ()
|/
* N0Ball -> FIX: typo of JASON ()
* N0Ball -> ADD: add JASON for demostrating how to commit. ()
You will never ever put an unworkable program on master.
git blame AUTHOR
AUTHOR
-> the target file to ask3add7ab5 (jason 2022-05-17 21:47:52 +0000 1) YOU ARE BAD
-L a, b
can be used for linesSTAGES
of git
Working Directory
OFTEN USED COMMAND
git checkout .
add
, .
for everything or you can type the filename you want to restoreecho "OUO" > test
cat test # prints out the content inside test
git checkout test
cat test
Staging Area
git restore --staged .
add
, .
for everything or you can type the filename you want to restoreecho "OUO" > test
git add .
git status
git restore --staged test
git status
REPO
git reset
--soft
-> only changing status--hard
-> changes everything back to the commit status
git reset --soft
+ git checkout .
echo "wrong commit" > test
git add .
git commit -m "OOPS: wrong commit"
git log
cat test
echo "right commit" > test
git reset --soft xxxxx
git log
cat test
git status
echo "original commit" > test
git add .
git commit -m "OOPS: wrong commit"
git log && cat test
echo "new commit" > test
git reset --hard xxxxx
git log && cat test
git status
COMMIT
git commit --amend
echo "test" > test
git add .
git commit -m "ADD: discription of T3ST"
git log
git commit --amend -m "ADD: description for TEST"
git log
OFTEN USED COMMAND
git clone <repo url>
At a high level, GitHub is a website and cloud-based service that helps developers store and manage their code, as well as track and control changes to their code. https://kinsta.com/application-hosting/
Clone methods
Go to Create a new repository
fill out the forms
You made it!