연습 - Git 사용해 보기

완료됨

첫 번째 리포지토리를 만들기 전에 Git이 설치되어 구성되어 있는지 확인해야 합니다. Git은 Azure Cloud Shell과 함께 미리 설치되므로 오른쪽의 Cloud Shell에서 Git을 사용할 수 있습니다.

Git 구성

  1. Cloud Shell에서 Git이 설치되었는지 다시 확인하려면 git --version을 입력합니다.

    git --version
    

    복사 단추를 사용하여 클립보드에 명령을 복사할 수 있습니다. 붙여넣으려면 Cloud Shell 터미널에서 새 줄을 마우스 오른쪽 단추로 클릭하고 붙여넣기를 선택하거나 Shift+Insert 바로 가기 키(macOS의 경우 ⌘+V)를 사용합니다.

  2. 다음 예제와 같은 출력이 표시됩니다.

    git version 2.7.4
    
  3. Git을 구성하려면 user.nameuser.email 전역 변수를 정의해야 합니다. 커밋을 수행하려면 둘 다 필요합니다.

  4. 다음 명령을 사용하여 Cloud Shell에서 사용자 이름을 설정합니다. <USER_NAME>을 원하는 사용자 이름으로 바꿉니다.

    git config --global user.name "<USER_NAME>"
    
  5. 이제 다음 명령을 사용하여 user.email 구성 변수를 만듭니다. <USER_EMAIL>을 사용자 메일 주소로 바꿉니다.

    git config --global user.email "<USER_EMAIL>"
    
  6. 다음 명령을 실행하여 변경 내용이 작동했는지 확인합니다.

    git config --list
    
  7. 출력에 다음 예제와 유사한 2줄이 포함되어 있는지 확인합니다. 사용자 이름과 메일 주소는 예제에 표시된 것과 다릅니다.

    user.name=User Name
    user.email=user-name@contoso.com
    

Git 리포지토리 설정

Git은 특정 폴더 내에서 파일의 변경 사항을 확인합니다. ‘작업 트리’(프로젝트 디렉터리)로 사용할 폴더를 만들고, 변경 내용 추적을 시작할 수 있도록 Git에 폴더를 알립니다. Git 리포지토리를 해당 폴더로 초기화하여 Git에 변경 사항 추적을 시작하라고 명령합니다.

먼저 프로젝트에 사용할 빈 폴더를 만든 다음 폴더 내부에서 Git 리포지토리를 초기화합니다.

  1. Cats라는 폴더를 만듭니다. 이 폴더는 프로젝트 디렉터리(작업 트리라고도 함)가 됩니다. 프로젝트 디렉터리에는 프로젝트와 관련된 모든 파일이 저장됩니다. 이 연습에서는 웹 사이트와 웹 사이트 및 해당 콘텐츠를 만드는 파일이 저장됩니다.

    mkdir Cats
    
  2. cd 명령을 사용하여 프로젝트 디렉터리로 변경합니다.

    cd Cats
    
  3. 이제 새 리포지토리를 초기화하고 기본 분기의 이름을 main으로 설정합니다.

    Git 버전 2.28.0 이상을 실행하는 경우 다음 명령을 사용합니다.

    git init --initial-branch=main
    

    또는 다음 명령을 입력합니다.

    git init -b main
    
    

    이전 Git 버전의 경우 다음 명령을 사용합니다.

    git init
    git checkout -b main
    
    

    initialize 명령을 실행하면 다음 예제와 유사한 출력이 표시됩니다.

    Initialized empty Git repository in /home/<user>/Cats/.git/
    
    Switched to a new branch 'main'
    
  4. 이제 git status 명령을 사용하여 작업 트리의 상태를 표시합니다.

    git status
    

    Git은 응답으로 main가 현재 분기임을 나타내는 출력을 표시합니다. (유일한 분기이기도 합니다.) 지금까지 아주 좋습니다.

     On branch main
    
     No commits yet
    
     nothing to commit (create/copy files and use "git add" to track)        
    
  5. ls 명령을 사용하여 작업 트리의 콘텐츠를 표시합니다.

    ls -a
    

    디렉터리에 .git이라는 하위 디렉터리가 있는지 확인합니다. Linux에서는 기본적으로 마침표로 시작하는 파일 및 디렉터리 이름이 숨겨지므로 ls와 함께 -a 옵션을 사용하는 것이 중요합니다. 이 폴더는 Git이 작업 트리의 기록과 메타데이터를 저장하는 디렉터리인 Git ‘리포지토리’입니다.

    일반적으로 .git 디렉터리에서 직접 작업을 수행하지는 않습니다. Git은 파일에서 변경된 내용을 추적하기 위해 작업 트리의 상태가 변경될 때 메타데이터를 업데이트합니다. 이 디렉터리는 사용자가 직접 사용하지 않지만 Git에는 매우 중요합니다.

Git에서 도움말 보기

대부분의 명령줄 도구처럼 Git에도 명령 및 키워드를 조회하는 데 사용할 수 있는 기본 제공 도움말 기능이 있습니다.

  1. Git으로 수행할 수 있는 작업에 대한 도움말을 보려면 다음 명령을 입력합니다.

    git --help
    
  2. 이 명령은 다음 출력을 표시합니다.

    usage: git [--version] [--help] [-C <path>] [-c name=value]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]
    
    These are common Git commands used in various situations:
    
    start a working area (see also: git help tutorial)
       clone      Clone a repository into a new directory
       init       Create an empty Git repository or reinitialize an existing one
    
    work on the current change (see also: git help everyday)
       add        Add file contents to the index
       mv         Move or rename a file, a directory, or a symlink
       reset      Reset current HEAD to the specified state
       rm         Remove files from the working tree and from the index
    
    examine the history and state (see also: git help revisions)
       bisect     Use binary search to find the commit that introduced a bug
       grep       Print lines matching a pattern
       log        Show commit logs
       show       Show various types of objects
       status     Show the working tree status
    
    grow, mark and tweak your common history
       branch     List, create, or delete branches
       checkout   Switch branches or restore working tree files
       commit     Record changes to the repository
       diff       Show changes between commits, commit and working tree, etc
       merge      Join two or more development histories together
       rebase     Forward-port local commits to the updated upstream head
       tag        Create, list, delete or verify a tag object signed with GPG
    
    collaborate (see also: git help workflows)
       fetch      Download objects and refs from another repository
       pull       Fetch from and integrate with another repository or a local branch
       push       Update remote refs along with associated objects
    
    'git help -a' and 'git help -g' list available subcommands and some
    concept guides. See 'git help <command>' or 'git help <concept>'
    to read about a specific subcommand or concept.
    

Git에서 사용할 수 있는 다양한 옵션을 읽어보고 자세히 살펴보려는 경우 각 명령과 함께 제공되는 도움말 페이지를 확인하세요. 아직 모든 명령을 이해하지는 못하겠지만 VCS를 사용한 경험이 있다면 익숙한 명령도 있을 것입니다.

다음 단원에서는 방금 시도한 명령 및 Git 기본 사항에 대해 자세히 알아봅니다.