文章目录
  1. 1. Install git.
  2. 2. Global Config
  3. 3. Initialize
  4. 4. Add files
  5. 5. Commit locally.
  6. 6. Generate SSH key.
  7. 7. Test your account
  8. 8. Connect to your repository and push
  9. 9. Revert changes made to your working copy
  10. 10. Revert changes made to the index (i.e., that you have added)
  11. 11. Revert a change that you have committed
  12. 12. How to show what a commit did
  13. 13. Create a new remote branch
  14. 14. Handle conflicts when pulling changes from remote
  15. 15. Diff local changes after staged
  16. 16. throw away the local commits, and pull the remote commits at origin/master
  17. 17. Ignore file mode differences
  18. 18. Delete the last commit
  19. 19. DIFF files between 2 different branches
  20. 20. 克隆一个仓库,不拿所有的历史
  21. 21. GIT clone branch only
  22. 22. 新建一个分支时,一定要记得加上
  23. 23. Ignore folder that accidentally committed
  24. 24. Git 协作流程
  25. 25. 常用 Git 命令清单

Install git.

Global Config

1
2
3
git config --global user.name "Your Real Name"
git config --global user.email [email protected]
git config --global core.filemode false

Initialize

1
2
3
4
mkdir my_git_repo
cd my_git_repo
git init
touch README.md

Add files

1
git add .

Commit locally.

1
git commit -m 'first commit.'

Generate SSH key.

1
ssh-keygen -C '[email protected]' -t rsa

Test your account

1

Connect to your repository and push

1
2
git remote add origin [email protected]:<username>/<projectname>.git
git push origin master

Revert changes made to your working copy

1
git checkout .

Revert changes made to the index (i.e., that you have added)

1
git reset

Revert a change that you have committed

1
git revert ...

How to show what a commit did

1
2
3
git show <commit-id>
git log -p
git log -p commit1 commit2

Create a new remote branch

1
git push -u origin <local-branch-name>:<remote-branch-name>

Handle conflicts when pulling changes from remote

1
2
3
4
git stash
git pull
<git merge, git commit> THIS STEP IS OPTIONAL
git stash pop

Diff local changes after staged

1
git diff --cached

throw away the local commits, and pull the remote commits at origin/master

1
2
git fetch origin
git reset --hard origin/master

Ignore file mode differences

1
2
3
git config core.fileMode false
git config --global core.autocrlf true
git config --local core.autocrlf true

Delete the last commit

1
2
git reset HEAD^ --hard
git push origin -f

DIFF files between 2 different branches

1
git diff mybranch master -- myfile.cs

克隆一个仓库,不拿所有的历史

1
git clone —depth 1 git://xxx/xxx.git

GIT clone branch only

1
git clone -b my branch --single-branch git://xxx/xxx.git

新建一个分支时,一定要记得加上

1
git branch --set-upstream-to branchName origin/branchName

  • As of Git 1.8.0:

1
git branch -u upstream/foo

Or, if local branch foo is not the current branch:

1
git branch -u upstream/foo foo

Or, if you like to type longer commands, these are equivalent to the above two:

1
2
git branch --set-upstream-to=upstream/foo
git branch --set-upstream-to=upstream/foo foo

  • As of Git 1.7.0:

1
git branch --set-upstream foo upstream/foo

Ignore folder that accidentally committed

1
git rm --cached .idea -r

Git 协作流程

常用 Git 命令清单

文章目录
  1. 1. Install git.
  2. 2. Global Config
  3. 3. Initialize
  4. 4. Add files
  5. 5. Commit locally.
  6. 6. Generate SSH key.
  7. 7. Test your account
  8. 8. Connect to your repository and push
  9. 9. Revert changes made to your working copy
  10. 10. Revert changes made to the index (i.e., that you have added)
  11. 11. Revert a change that you have committed
  12. 12. How to show what a commit did
  13. 13. Create a new remote branch
  14. 14. Handle conflicts when pulling changes from remote
  15. 15. Diff local changes after staged
  16. 16. throw away the local commits, and pull the remote commits at origin/master
  17. 17. Ignore file mode differences
  18. 18. Delete the last commit
  19. 19. DIFF files between 2 different branches
  20. 20. 克隆一个仓库,不拿所有的历史
  21. 21. GIT clone branch only
  22. 22. 新建一个分支时,一定要记得加上
  23. 23. Ignore folder that accidentally committed
  24. 24. Git 协作流程
  25. 25. 常用 Git 命令清单