Skip to content
12tall edited this page May 7, 2020 · 3 revisions

Git is a distributed version control system. It has two basic function: back up your work and cooperative development.

basic

There are some commands which are used frequently, if you want to command Git well:

# basic configurations after git installed
# generate a ssh-key(id_rsa.pub) in ~/.ssh/ or C:\Users\USERNAME\.ssh
# which will be used soon
ssh-keygen
# global configurations, you can also modified in each repo
git config --global user.name "12tall"
git config --global user.email "12tall@domain.com"

# initiate a git repository at current path 
git init  # you can see a hidden directory, which is named .git
# add temp files
git add filename  
git add ./sub/filename 
git add .  # add all changed files
# commit  
git commit -m "some comments"

# work with remote server. (github, gogs, etc.
# register an account 
# copy the content of id_rsa.pub, and paste in the server
git clone git@github.com:12Tall/toys.git  # clone a repository  
git remote add origin git@github.com:12Tall/toys.git  # add a remote repo. it is not needed when using a cloned repo.
# git add .
# git commit -m "sth."
git push -u origin master  # push to remote server

back up

git log  # show your commit logs. press 'q' to quit
git reset --hard HEAD^  # back to last commit  
git reset --hard 1094a  # back to a specified commit, '1094a' is a part of guid, which every commit has an unique one.
git reflog  # show the command history

cooperation

These commands above are basic, and used most frequently. And there are some advanced instruction for cooperation:
branchs
ref: https://www.liaoxuefeng.com

# branch: think is as time line
# assume in the workspace  
# yeah, the default branch is 'master'
git checkout -b dev  # create and switch to a new branch 'dev'
# and the modify you did in 'dev' will not affect the files in 'master'  
# git checkout -b dev equals:
git branch dev  # create 'dev'
git checkout dev  # switch to an existed branch 'dev'  

# when you have done the works in 'dev'(have several commits), you should 
git checkout master  # back to 'master'
git merge dev  # merge 'dev' to 'master'  
# there might be some conflictions when you are trying to merge code.
# because someone modified the 'master' branch.
# but don't worry, we can fix it manually. [little bit of complex]
git branch -d dev  # remove 'dev'  

# other instructions
git switch -c dev # same with 'git checkout -b dev'
git switch master # same with 'git checkout master'

note

  • do not change other's code in the master branch. you should create a new branch, let the author to merge it.

Clone this wiki locally