본문 바로가기
카테고리 없음

[Linux] Git 초기 연결 & 간단 사용법

by 쿡노트 2024. 3. 7.
반응형

Git 저장소는 주로 다음 두 가지 중 한 가지 방법으로 Git 저장소를 쓰기 시작한다고 한다. 
이미 Git 원격 저장소를 구성해놓은 상태에서 두 번째 방법에 대해 기재한다.

 

1. 아직 버전관리를 하지 않는 로컬 디렉토리 하나를 선택해서 Git 저장소를 적용하는 방법
2. 다른 어딘가에서 Git 저장소를 Clone 하는 방법

 

환경

원격저장소 : GitLab

로컬저장소 : Linux Server (CentOS 7.9)

 

Git 이란?

주로 소프트웨어의 소스 코드를 버전 관리하는 분산 버전 관리 시스템(DVCS)이다.
**버전 관리 시스템(VCS - Version Control System)

 

Git의 장점

Git은 대부분 Clinet에서 작업한다.(로컬 저장소) remote 저장소를 (history를 포함하여) 전부 로컬 저장소로 복제(Clone) 하며, 거의 모든 명령어가 로컬에서 수행되어 속도가 빠르다.

또, 로컬 저장소로 복제(Clone)하기 때문에 원격저장소 서버에 문제가 생기면 클라이언트 중에서 아무거나 골라도 서버를 복원할 수 있다.(참조 : https://git-scm.com/book/ko/v2/)

 

GitLab?

Git 저장소 및 CI/CD, 이슈 추적, 보안성 테스트 등의 기능을 갖춘 웹 기반의 데브옵스 플랫폼이다. (참조 : https://ko.wikipedia.org/wiki/%EA%B9%83%EB%9E%A9)

Git 설치 확인

[root@localhost ~]# git --version
git version 1.8.3.1

--설치가 안되어 있는 경우 CentOS 기준으로 yum install로 설치 해 주면 된다.
[root@localhost ~]# yum install git

Git 최초 설정

사용자 정보

**global 은 전역 옵션

$ git config --global user.name "user"
$ git config --global user.email "user@gmail.com"

Git 설정 확인

$ git config --list

**git repository가 아닌 디렉터리에서 조회하면, global 옵션만 확인된다.

[user@localhost ~]$ git config --list
user.name=user
user.email=user@gmail.com

--global gitconfig 설정 파일 경로
[user@localhost ~]$ pwd
/home/user
[user@localhost ~]$ cat .gitconfig
[user]
name = user
email = user@gmail.com

Git 저장소 만들기

$ git init

** git repository를 생성하고자 하는 폴더로 이동 후 git init 수행 (원격저장소와 연결 안 된 빈 폴더를 생성)

--테스트 디렉터리 생성
$ cd /home/user/
$ mkdir git_test
$ cd git_test
[user@localhost git_test]$ git init

Initialized empty Git repository in /home/user/git_test/.git/

로컬 저장소, 원격 저장소 연결

$ git remote add 원격저장소 연결명 "원격저장소  URL"

$ git remote add origin "http://192.168.100.100:8080/username/myproject"

-- git remote -v  로컬 저장소와 원격저장소 연결 여부 확인

$ git remote -v
origin http://192.168.100.100:8080/username/myproject/ (fetch)
origin http://192.168.100.100:8080/username/myproject/ (push)

--git config --list로  remote 서버 정보 추가 된 내역을 확인할 수 있다. 

$ cd /home/user/git_test
$ git config --list
user.name=user
user.email=user@gmail.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=http://192.168.100.100:8080/username/myproject/
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*

--git repository 설정 파일(config) 확인

$ cd /home/user/git_test/.git
$ cat config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = http://192.168.100.100:8080/username/myproject/
fetch = +refs/heads/*:refs/remotes/origin/*

Git clone

GitLab UI에서 git clone 주소 확인 가능
Repository → Files → Clone 클릭 → Clone Click

 

$ git clone 원격저장소URL.git 디렉터리명 

$ git clone http://192.168.100.100:8080/username/myproject.git

생성 디렉터리 : myproject

--Git Clone시 폴더명 지정
$ git clone http://192.168.100.100:8080/username/myproject.git myproject2

생성 디렉터리 : myproject2

(pull) 원격 서버의 Commit 당겨오기

$ git pull

$ git pull

--(참고)패스워드 저장
git pull 이전에 아래 명령어를 수행하고, git pull 명령어 수행시 ID/Password 입력하면 향후 ID/Password를 입력 하지 않아도 된다.
원격 저장소에서 Clone 받고 난 myproject2 디렉터리 이동 후 수행
$ cd /home/user/myproject2/ 
$ git config credential.helper store
$ git pull

(push) 신규 파일 생성 후 원격 서버에 올리기(순서 : git add -> git commit -> git push)

1. $ git add test.py Staging 영역에 변경 내역 추가

--테스트 파일 생성 test.py
cat <<EOF > /home/user/git_test/myproject2/test.py
print("git push test")
EOF

-- Git 상태 확인
$ git status

# On branch main
# Untracked files:
#   (use "git add ..." to include in what will be committed)
#
# test.py
nothing added to commit but untracked files present (use "git add" to track)

--git add
$ git add test.py

$ git status

# On branch main
# Changes to be committed:
#   (use "git reset HEAD ..." to unstage)
#
# new file:   test.py
#
git commit -m "new file commit test"

2. $ git commit -m "command" Staging 영역의 변경된 내역 묶음 및 정의

$ git commit -m "new file commit test"

[main cbe3ccc] new file commit test
 1 file changed, 1 insertion(+)
 create mode 100644 test.py

$ git status

# On branch main
# Your branch is ahead of 'origin/main' by 1 commit.
#   (use "git push" to publish your local commits)
#
nothing to commit, working directory clean

3. $ git push origin main 원격저장소 반영

$ git push origin main

Counting objects: 4, done.
Delta compression using up to 32 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 281 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To http://192.168.100.100:8080/username/myproject.git
   a1f2f2b..2e98954  main -> main

로컬 저장소 파일 삭제 후 원격저장소 반영

1. $ rm test.py

2. $ git commit -m "test file deleted"

3. $ git push origin main

 

감사합니다.

반응형