Git

Ubuntu 에서 Git 사용하기

Jack Moon 2022. 6. 30. 12:25

1. Ubuntu에 git 설치

$ sudo apt update
$ sudo apt install git

2. 버전확인 및 초기설정

$ git --version
git version 2.25.1
$ git config --global user.name "Jack Moon"
$ git config --global user.email "moon0sool@daum.net"

3. 폴더 생성및 저장소 만들기 (이 폴더를 기준으로 버전관리를 함)

$ mkdir git-test
$ cd git-test/
$ git init
$ ls -al
total 12
drwxrwxr-x 3 jack jack 4096 Jun 29 16:45 .
drwxr-xr-x 9 jack jack 4096 Jun 29 16:45 ..
drwxrwxr-x 7 jack jack 4096 Jun 29 16:45 .git

 

4. First commit

$ touch README.md
$ git add README.md
$ git commit -m "first commit"

git status 는 현재의 상태와 이전으로 가는 명령을 알려준다.

$ vi README.md # 수정 후 저장
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   README.md
no changes added to commit (use "git add" and/or "git commit -a")
$ git add README.md 
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   README.md
$ git commit -m "second"
[master 0f9a7e4] second
 1 file changed, 1 insertion(+)
$ git status
On branch master
nothing to commit, working tree clean

5. 상태확인

현재 파일 상태 확인
$ git status
변경사항 확인
$git diff
commit 히스토리 조회
$ git log

6. 저장소에서 무시할 파일 설정

.gitignore 에 버전관리에서 제외할 파일을 추가

build/		# build 폴더에 있는 파일 무시
.swp		# 확장자가 swp인 파일 무시
doc/*.txt	# doc 폴더에 있는 모든 txt 파일 무시

7. 버전 복구 (특정 상태로 이동)

$ git log
$ git reset --hard d28065051a29abfc0c7c6bedb5556562f5e0a945

8. 특정파일만 되돌리기

first.txt 수정 이전의 commit id 를 찾아 
git checkout <commit_id> <file_name>

1. 첫 파일 생성
$ vi first.txt  # 첫 파일 생성
$ git add first.txt  # 스테이지에 추가
$ git commit -m "Create first.txt"  # commit
2. 두번째 파일 생성
$ vi second.txt
$ git add *
$ git commit -m "Create second.txt"
3. 세번째 파일 생성
$ vi third.txt
$ git add third.txt 
$ git commit -m "Create third.txt"
4. 첫 파일 수정
$ vi first.txt 
$ git add first.txt 
$ git commit -m "Modify first.txt 멀티라인"
5. 첫 파일 수정사항 되돌리기
$ git log
$ git checkout 3fc1f753747de6317e3d25053b336aa45c163719 first.txt
Updated 1 path from b11ffa3
상태를 보면 스테이징 상태이므로 commit 으로 버전 확정
$ git status
On branch master
Your branch and 'origin/master' have diverged,
and have 4 and 3 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   first.txt
$ git commit -m "first.txt 수정 전 상태로 이동"
[master 68a6df2] first.txt 수정 전 상태로 이동
 1 file changed, 1 insertion(+), 5 deletions(-)
6. 다시 수정 상태로 이동하고 싶으면
$ git log
$ git checkout 644a3b1491e1668ad9ef0914d2e23ecea3b95ad0 first.txt
$ git commit -m "first.txt 다시 수정"

'Git' 카테고리의 다른 글

Git 정리  (0) 2017.05.25