? ǰ��

Git���ִ����������в��ɻ�ȱ�İ汾���ƹ��ߡ��������dz�ѧ�߻����о���Ŀ����ߣ�����Git���DZر����ܡ����Ľ�������㿪ʼ��ϵͳѧϰGit�ĸ��ֲ��������ʵ����

? ѧϰĿ��

ͨ������ѧϰ���㽫���գ�

  • Git�Ļ�������͹���ԭ��
  • ����Git�����ʹ�÷���
  • ��֧�����ͺϲ�����
  • Զ�ֿ̲�IJ���
  • �����ͻ�ļ���
  • Git�������̺����ʵ��

? Git��������

ʲô��Git��

Git��һ���ֲ�ʽ�汾����ϵͳ����Linux֮��Linus Torvalds�����������������ص㣺

  • �ֲ�ʽ��ÿ�������߶��������Ĵ�����ʷ
  • ��Ч�����ٵķ�֧�����ͺϲ�
  • ��ȫ��ʹ��SHA-1��ϣȷ������������
  • �����֧�ֶ��ֹ�������

Git����������

1
2
3
4
5
6
7
������ (Working Directory)
�� git add
�ݴ��� (Staging Area)
�� git commit
���زֿ� (Local Repository)
�� git push
Զ�ֿ̲� (Remote Repository)

?? Git��������

1. ��ʼ��������

1
2
3
4
5
6
7
8
9
# ��ʼ���ֿ�
git init

# �����û���Ϣ
git config --global user.name "�������"
git config --global user.email "�������"

# �鿴����
git config --list

2. ���������

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# �鿴״̬
git status

# �����ļ����ݴ���
git add filename.txt # ���ӵ����ļ�
git add . # ���������ļ�
git add *.js # ��������js�ļ�

# �ύ����
git commit -m "�ύ��Ϣ"
git commit -am "���Ӳ��ύ" # �����ݴ���ֱ���ύ

# �鿴�ύ��ʷ
git log
git log --oneline # ����ʽ
git log --graph # ͼ�λ���ʾ

3. �ļ�״̬����

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# �����������޸�
git checkout -- filename.txt

# �����ݴ����ļ�
git reset HEAD filename.txt

# �����ύ
git reset --soft HEAD~1 # �����޸�
git reset --hard HEAD~1 # �����޸�

# �鿴�ļ�����
git diff # ���������ݴ���
git diff --cached # �ݴ�����ֿ�
git diff HEAD # ��������ֿ�

? ��֧����

��֧��������

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# �鿴��֧
git branch # ���ط�֧
git branch -r # Զ�̷�֧
git branch -a # ���з�֧

# ������֧
git branch feature-login # ������֧
git checkout -b feature-login # �������л�

# �л���֧
git checkout main
git switch main # Git 2.23+������

# �ϲ���֧
git checkout main
git merge feature-login

# ɾ����֧
git branch -d feature-login # ɾ���Ѻϲ���֧
git branch -D feature-login # ǿ��ɾ��

��֧�ϲ�����

1
2
3
4
5
6
7
8
# Fast-forward�ϲ���Ĭ�ϣ�
git merge feature-branch

# �����ϲ��ύ
git merge --no-ff feature-branch

# ����ϲ�
git rebase main

? Զ�ֿ̲����

Զ�ֿ̲����

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# ����Զ�ֿ̲�
git remote add origin https://github.com/username/repo.git

# �鿴Զ�ֿ̲�
git remote -v

# ���͵�Զ��
git push origin main
git push -u origin main # �������η�֧

# ��Զ����ȡ
git pull origin main # ��ȡ���ϲ�
git fetch origin # ֻ��ȡ���ϲ�

# ��¡�ֿ�
git clone https://github.com/username/repo.git

Զ�̷�֧����

1
2
3
4
5
6
7
8
# �����·�֧
git push origin feature-branch

# ɾ��Զ�̷�֧
git push origin --delete feature-branch

# ����Զ�̷�֧
git checkout -b local-branch origin/remote-branch

? �߼�����

1. ����(Stash)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# ���ص�ǰ�޸�
git stash
git stash save "������Ϣ"

# �鿴�����б�
git stash list

# Ӧ�ô���
git stash apply # Ӧ�����´���
git stash apply stash@{1} # Ӧ��ָ������

# ɾ������
git stash drop
git stash clear # ��������

2. ��ǩ����

1
2
3
4
5
6
7
8
9
10
11
# ������ǩ
git tag v1.0.0
git tag -a v1.0.0 -m "�汾1.0.0"

# �鿴��ǩ
git tag
git show v1.0.0

# ���ͱ�ǩ
git push origin v1.0.0
git push origin --tags

3. ���

1
2
3
4
5
6
7
8
9
# �������
git submodule add https://github.com/user/repo.git path/to/submodule

# ��ʼ����ģ��
git submodule init
git submodule update

# �������
git submodule update --remote

? ��ͻ���

�ϲ���ͻ����

�����ֳ�ͻʱ��Git�����ļ��б�dz�ͻ����

1
2
3
4
5
<<<<<<< HEAD
��ǰ��֧������
=======
Ҫ�ϲ���֧������
>>>>>>> feature-branch

������裺

  1. �ֶ��༭��ͻ�ļ�
  2. ɾ����ͻ���
  3. ���ӵ��ݴ�����git add filename
  4. �ύ�ϲ���git commit

���ó�ͻ�������

1
2
3
4
5
6
7
8
9
10
11
# �鿴��ͻ�ļ�
git status

# ʹ�ù��߽����ͻ
git mergetool

# ��ֹ�ϲ�
git merge --abort

# ��ֹ���
git rebase --abort

? Git��������

1. Git Flow

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# ���ܿ�������
git checkout -b feature/new-feature develop
# ��������...
git checkout develop
git merge --no-ff feature/new-feature
git branch -d feature/new-feature

# ��������
git checkout -b release/1.0.0 develop
# �޸�bug...
git checkout main
git merge --no-ff release/1.0.0
git tag v1.0.0
git checkout develop
git merge --no-ff release/1.0.0

2. GitHub Flow

1
2
3
4
5
6
# ������
git checkout -b feature-branch
# ��������...
git push origin feature-branch
# ����Pull Request
# ��������ϲ���main

? ���ʵ��

�ύ��Ϣ�淶

1
2
3
4
5
6
7
8
# �õ��ύ��Ϣ��ʽ
feat: �����û���¼����
fix: �޸�������֤bug
docs: ����API�ĵ�
style: ��ʽ������
refactor: �ع��û�ģ��
test: ���ӵ�Ԫ����
chore: ����������

��֧�����淶

1
2
3
4
feature/user-authentication    # ���ܷ�֧
bugfix/login-error # �޸���֧
hotfix/security-patch # ���޸���֧
release/v1.2.0 # ������֧

.gitignore����

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# ����Ŀ¼
node_modules/
vendor/

# �������
dist/
build/
*.min.js

# ��־�ļ�
*.log
logs/

# ��������
.env
.env.local

# IDE�ļ�
.vscode/
.idea/
*.swp

# ϵͳ�ļ�
.DS_Store
Thumbs.db

? ����������

1. ��������

1
2
3
4
5
6
7
8
# �������һ���ύ�������޸�
git reset --soft HEAD~1

# �޸����һ���ύ��Ϣ
git commit --amend -m "�µ��ύ��Ϣ"

# �����ļ��޸�
git checkout -- filename

2. �����ֿ�

1
2
3
4
5
6
7
# ����δ�����ļ�
git clean -f # ɾ���ļ�
git clean -fd # ɾ���ļ���Ŀ¼
git clean -n # Ԥ��Ҫɾ��������

# ��������
git gc

3. �鿴��ʷ

1
2
3
4
5
6
7
8
9
# �鿴�ļ��޸���ʷ
git log -p filename

# �鿴ij�д�����޸���ʷ
git blame filename

# �����ύ
git log --grep="�ؼ���"
git log --author="������"

?? Git��ȫ

1. ǩ���ύ

1
2
3
4
5
6
# ����GPGǩ��
git config --global user.signingkey YOUR_GPG_KEY
git config --global commit.gpgsign true

# ǩ���ύ
git commit -S -m "ǩ���ύ"

2. ������Ϣ����

1
2
3
4
# ����ʷ��ɾ�������ļ�
git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch secrets.txt' \
--prune-empty --tag-name-filter cat -- --all

? ѧϰ��Դ

�Ƽ���Դ

ʵ�ù���

  • GUI����: GitKraken, SourceTree, GitHub Desktop
  • ��������ǿ: Oh My Zsh, Git����
  • ������ϰ: Katacoda Git�γ�

? �ܽ�

Git���ִ��������ɻ�ȱ�Ĺ��ߣ���������Ҫ����ѧϰ��ʵ�����ϡ��ӻ�����add��commit��push��ʼ����ѧϰ��֧��������ͻ����ȸ߼����ܡ�

��ס��ЩҪ�㣺

  • Ƶ���ύ�������ύ��������
  • д�������ύ��Ϣ
  • ����ʹ�÷�֧���й��ܿ���
  • ����ͬ��Զ�ֿ̲�
  • �������ⲻҪ�ţ�Git�������в��������Գ���

������ϰ����ᷢ��Git�����ǰ汾���ƹ��ߣ�������߿���Ч�ʵ�������


? С��ʿ: ������ѧϰ�����д���һ����ϰ�ֿ⣬���ֲ���ÿ����������ܸ��õ�����Git�Ĺ���ԭ����

��������Ƽ�: