컴퓨터 과학/[zerobase] Git & GItHub

Chapter 04. Git SetUp

계란💕 2022. 10. 5. 15:27

4. Git 셋업하기

 

4.1 사용자 설정 및 세팅 - 이론

  • 로컬에서 사용할 Git 사용자 이메일과 이름을 설정
  • git config: Git에 관한 설정을 추가, 변경, 삭제하는 명령어
  • 설정 파일
    • System 설정 파일(모든 시스템 사용자에게 적용) , ex) git config --system 
    • Global 설정 파일(한 사용자의 전치 Git Repository에 적용된다.) , ex) git config --global
    • Local설정 파일(하나의 Repository에만 적용된다. ) , ex) git config --local

 

 

  Note) Global Git 사용자 설정과 확인

  • git config --global user.email "내 이메일 주소 - 깃 아이디"
  • git config --global user.name "내 이름"
  • 설정 확인:  git config --list

 

 

  Note) GitHub 계정에 ssh key 등록하기

  • 컴퓨터에 ssh 키를 만들어서 깃허브 계정에 key 값을 등록한다. - 사용자를 인식한다.
  • GitHub 오른쪽 상단 프로필 클릭, setting -> ssh and GPG keys
  • new SSH Key클릭하여 Title과 복사한 key 입력한 다음 add SSH key 클릭

 

cf) SSH(Secure SHell, 시큐어 셸)

  • 네트워크 상의 다른 컴퓨터에 로그인 하거나 원격 시스템에서 명령을 실행하고 다른 시스템으로 파일을 복사할 수 있도록 해주는 응용 프로그램 또는 그 프로토콜을 말한다.
  •  

 

 

 

 

4.2 사용자 설정 및 세팅 - 실습

 

 

  Note) ssh 공개키 만들어서 깃허브 계정에 등록하기

https://git-scm.com/book/ko/v2/Git-%EC%84%9C%EB%B2%84-SSH-%EA%B3%B5%EA%B0%9C%ED%82%A4-%EB%A7%8C%EB%93%A4%EA%B8%B0

 

Git - SSH 공개키 만들기

많은 Git 서버들은 SSH 공개키로 인증한다. 공개키를 사용하려면 일단 공개키를 만들어야 한다. 공개키를 만드는 방법은 모든 운영체제가 비슷하다. 먼저 키가 있는지부터 확인하자. 사용자의 SSH

git-scm.com

 

  • cd ~/.ssh: ssh 키가 저장된 루트 폴더로 이동한다.
  • ls를 입력하면 생성한 파일 목록이 나온다.
  • ssh-keygen 입력한 다음, 파일 이름, 비밀번호를 입력해서 새로운 SSH Key를  생성한다. (비번은 비어있어도 된다.)
    • ls를 입력해서 파일 목록을 확인한다.

 

  • cat ~/.ssh/gitlecture.pub 입력하면 나오는 긴 코드를 깃허브에 등록해야한다.
    • 만약, 이 부분에서 ssh-key가 나오지 않으면 창을 껐다가 켜면 해결된다.

      -> 깃허브 -> Settings -> Access 탭 아래에 SSH and GPG keys -> new SSH Key 클릭

      -> 그러면 내 깃허브 계정에 키가 등록된 걸 볼 수 있다.

 

 

 

 

4.2 Git 초기화 및 삭제

 

  • 초기화 명령어: git init
  • 초기화란 어떤 폴더에 속하는 모든 파일에 대해 깃으로 관리하겠다고 선언하는 것을 의미한다.
    • 소스트리에서는 "create"가 깃 초기화를 의미한다.
  • 초기화하려면 폴더 안에서 명령어 git init을 입력한다.
  • 초기화하고 나면 폴더 안에 숨김 폴더로 .git 폴더가 생긴다.
    • .git 폴더를 삭제하면 모두 지워진다.
  • Mac 또는 Linux 환경: rm -rf .git  =>  .git 폴더 삭제 
    • 그러면 해당 폴더의 변경 사항을 Git이 더이상 관리하지 않는다.

 

 

 

  Ex) 초기화, 삭제

  • git init 입력해서 초기화한다.
  • ls -al로 조회해보면 새롭게 '.git'폴더가 생긴걸 볼 수 있다. 로컬에 대한 설정 파일이 들어있다.
    • 프로젝트에 대한 설정 파일  config가 들어있다.

    

      -> 이전에 설정했던 global 사용자 설정의 영향으로 "config"가 있다.

 

 

  • rm -rf .git 입력한다.

  

 

 

 

4.3 .gitignore란?

  • .gitignore: 사용자가 git에 등록(커밋)되지 않길 바라는 파일 또는 폴더들의 목록을 저장한다.(자주 안 쓰는 파일들)
  • .gitignore에 등록된 파일(폴더)들은 커밋 시 자동으로 제외된다.

 

 

gitignore작성 방법

  •  '#' 은 주석 역할을 한다.
  • 폴더: /폴더명     ex) /docs
  • 파일: 파일명.확장자    ex) test.txt
  • 폴더 안의 파일: /폴더명/파일명.확장자    ex)  /dosc/test.txt
  • 폴더 안 특정 확장자 파일 전부    ex) /docs/*.text
    • 이 때, docs 내부의 폴더에 대해서는 영향이 없다.
  • 폴더 안의 파일 뿐만 아니라 폴더의 하위 폴더 안에 있는 모든 특정 확장자 파일 전부    ex) /docs/**/*.txt

 

 

  ex) gitignore.io 사이트에 접속하면 작성에 유용하다.

  • 인텔리제이 검색 =>  gitignore
<hide/>
# Created by https://www.toptal.com/developers/gitignore/api/intellij
# Edit at https://www.toptal.com/developers/gitignore?templates=intellij

### Intellij ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# AWS User-specific
.idea/**/aws.xml

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn.  Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# SonarLint plugin
.idea/sonarlint/

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

### Intellij Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721

# *.iml
# modules.xml
# .idea/misc.xml
# *.ipr

# Sonarlint plugin
# https://plugins.jetbrains.com/plugin/7973-sonarlint
.idea/**/sonarlint/

# SonarQube Plugin
# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin
.idea/**/sonarIssues.xml

# Markdown Navigator plugin
# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced
.idea/**/markdown-navigator.xml
.idea/**/markdown-navigator-enh.xml
.idea/**/markdown-navigator/

# Cache file creation bug
# See https://youtrack.jetbrains.com/issue/JBR-2257
.idea/$CACHE_FILE$

# CodeStream plugin
# https://plugins.jetbrains.com/plugin/12206-codestream
.idea/codestream.xml

# Azure Toolkit for IntelliJ plugin
# https://plugins.jetbrains.com/plugin/8053-azure-toolkit-for-intellij
.idea/**/azureSettings.xml

# End of https://www.toptal.com/developers/gitignore/api/intellij

 

  • visual studio code
<hide/>
# Created by https://www.toptal.com/developers/gitignore/api/visualstudiocode
# Edit at https://www.toptal.com/developers/gitignore?templates=visualstudiocode

### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets

# Local History for Visual Studio Code
.history/

# Built Visual Studio Code Extensions
*.vsix

### VisualStudioCode Patch ###
# Ignore all local history of files
.history
.ionide

# Support for Project snippet scope
.vscode/*.code-snippets

# Ignore code-workspaces
*.code-workspace

# End of https://www.toptal.com/developers/gitignore/api/visualstudiocode

 

 

 

  Ex) gitignore 실습

어둡게 (기본 어둡게) - 반영됐는지 확인 불가
어둡게 (vs code)로 설정해야 회색으로 바뀐 걸 확인 가능하다.
설정 - 색 테마

 

 

  •  위 코드를 프로젝트의 .gitignore 파일에 저장한다.
  • .gitignore 파일에 지우고 싶은 파일명을 적은 다음 저장한다. 그러고나서 파일 목록을 보면 색이 파일명 색이 회색으로 변한다.

 

 

  • 폴더 안의 특정 파일만 ignore 하려면?