Git & Github

JunePyo Suh·2020년 4월 29일
0

Overview

VCS (Version Control System) controls versions of source codes.

The simplest way would be using different file names.
A complex and fancy way would be using a distributed version control system, but running a server to operate VCS is costly and inefficient for many organizations.

That is why we use Git and Github.
Github is the central server, and git is the local environment.
By using the git-clone command, source codes in the central server can be transported to the local environment.
In order to upload to the central server, use git push command.
If another developer decides to download something that has been modifed and updated by me, he or she would need to pull the file.

Version States

Git을 사용해서 파일 버전 관리를 할때 파일은 다음 3개의 상태중 하나의 상태에 있게 됩니다.

  • Modified: 수정되고 commit 되지 않은 파일.

  • Staged: 곧 commit 될거라고 mark 해놓은 상태. 이렇게 중간 상태가 존재하는 이유는 commit 하기전에 중간 상태를 저장할 수 있도록 하기 위함. Commit을 하고 나면 다시 되돌려야 할때 까다롭기 때문임.

  • Committed: 수정 사항들이 git에 저장이 된 상태. Commit이 되었을때 비로소 push를 할수있는 준비가 되었다고 말함.

Git을 사용한 버전 관리 Flow

Git 기본 명령어

git init: 프로젝트를 git repository로 만들기 위해 사용. git init을 해서 git repo로 만들어야 git으로 버전 관리가 시작됨.

git add: modified 파일들을 staged 상태로 옴기고자 할때 사용하는 명령어. Add를 하지 않으면 git에서는 commit 을 할 수 없음.

git commit: staged 된 파일들을 하나의 commit 으로 만들어서 commit 메세지를 부여하고 commit을 하는 작업임.

git diff: modified 상태에서만 결과물이 나오며, 수정사항들을 보여줌.

git status: 현재 git repository 안에 있는 상태를 보여줌. 현재 상태가 modified 인지 staged 인지...

git log: 커밋 히스토리를 보여줌.

git rm: git repo 에 등록된 파일을 삭제하는데 사용함

git mv: 파일을 이동하거나 rename 할때 사용함

git branch: branch를 생성, 관리, 삭제할때 사용

git checkout: branch를 이동할대 사용하는 명령어

git remote -v: remote로 연결된 중앙 서버가 있는지 확인 (github 확인)

gi status 는 현재 git의 상태를 보여줌.

git rm--cached fileName: 이미 add된 파일을 되돌릴때 사용
git reset fileName: 이미 commit 된 파일을 되돌릴때 사용
git checkout -- fileName: modified 된 git 을 원상복귀 시켜줌 (add되기 전 상태)

git remote add origin https://github.com/junePyo/project_name.git
(origin 은 remote 주소의 별칭으로 사용됨)

git push origin branch/name: push 할때 사용

git에 user info 전달하기

*** 내가 누구인지 설정하십시오.

다음을 실행하면,

  git config --global user.email "you@example.com"
  git config --global user.name "내 이름"

계정의 기본 신원 정보를 설정합니다.
--global 옵션을 빼면 이 저장소서만 신원 정보를 설정합니다.

fatal: 메일 주소를 자동 검사할 수 없습니다 

git config --global user.email "you@example.com"
git config --global user.name "내 이름"

Branch & merging

git 은 commit 내용을 tree 형태로 관리합니다. Master는 소스 branch 이며, 수정을 하고 싶을 때는 master 를 사용하지 않고 다른 branch 를 만들어 수정합니다. 이렇게 생성하는 branch 는 feature branch 라고 부릅니다.
생성시 명칭은 feature-branchName 꼴로 만드는 것이 convention 입니다.

merge 는 기존의 코드와 수정사항 커밋을 합치는 과정을 말하는데, 이 과정은 master branch 에 기능 branch 에서 작업한 커밋을 병합하는것을 말합니다. 충돌이 발생할수도 있는데, conflict 가 발생하면 해당 부분을 git에서 가이드 해주니, 그 가이드를 확인하고 이후 최종 수정할때는 파일에서 지워야 합니다.

Branching and merging process

  1. Checkout the Project OR Initialize Git
    1-1. Checkout: git clone https://github.address.com --> move to git folder that has been created
    1-2. Initialie git: "git init" command from the repository that you wish to activate git

  2. Create branch
    #create branch
    git branch feature/edit
    #look up existing branches
    git branch
    #switching branches
    git checkout feature/edit
    #create and switch to new branch
    git checkout -b feature/edit
    #delete branch
    git branch -d feature/edit

  3. Do your work
    3-1. If you wish to create new file:

  • touch newFile.py
  • vim newFile.py
  • git add newFile.py OR git add .
  • git commit -m "add newFile" OR just type git commit, which automatically moves to the file and lets the user write any message regarding the commit
  1. git push origin feature/edit (branch name)

  2. Pull request on Github --> Merged if no conflict

  3. git pull origin master from the local master branch (This part is important, since the user's local environment is not aware of changes made to the central server master branch)

If there is conflict
If the conflict is due to the master branch being updated while the modification process was occurring in the user's local environment:

  1. pull any new changes made to the master branch to current working branch
    git pull origin master

  2. make additional changes to avoid conflict
    * git will be providing guidelines

  3. add, commit, push

  4. start another pull request on Github

  5. Conflict resolved, merge takes place

After the pull request has been approved
Once a git branch is successfully used in calling pull request and getting approval, git no longer watches the branch. Changes made through this branch will no longer be tracked, so either delete the branch and start working from a new branch with the same name, or just start a new branch with a new name.

.gitignore
A .gitignore file is a plain text file where each line contains a pattern for files/directories to ignore. Generally placed in the root folder of the repository. The patterns in the files are relative to the location of the .gitignore file.
The purpose of the .gitignore file is to allow the user to ignore files, such as editor backup files, build products or local configuration overrides that he or she never wants to commit into a repository.

Git 추가정보

0개의 댓글