2018-01-23
필자는 git을 사용하고는 있지만, 이게 한번 셋팅을 해놓으면, 다시 셋팅할때까지 텀이 꽤 되기 때문에, 사용할때마다 항상 헷갈리는 것도 많고, 까먹기도 하고 해서 이곳에 기본적인 사용예를 조금 자세히 적어 놓을 생각이다.

일단 상황은, 이렇다.
test_server라는 기존에 사용중이 서버의 메인 디렉토리가 있다.
이런 상황에서 새로운 remote저장소에 test_server디렉토리를 push 할 수 있게 하려고 한다.

우선 test_server폴더를 git저장소로 만든다.
# cd test_server # git init Initialized empty Git repository in /test_server/.git/ # ls -al drwxr-xr-x 3 root root 17 1월 23 11:57 . drwxr-xr-x 5 root root 64 1월 23 11:57 .. drwxr-xr-x 7 root root 111 1월 23 11:57 .git . . .
기존의 test_server에 있는 파일들을 전부 git에 올려놓고 싶으면, git add * 이라고 하면 된다. 하지만 보통 upload 디렉토리나 *.log파일같은 서버 내에서만 필요하거나 생성되는 것들은 git 저장소에 올려놓지 않는다.
그럴때 필요한게 .gitignore파일이다.
.gitignore파일을 만들고 아래와 같이 git저장소에 add하지 않을 파일이나 디렉토리를 적어 놓는다.
upload/ *.log
이제 git add . 명령으로 모든 파일들을 git 저장소에 저장하면 된다.

참고로 .gitignore파일에 위와같이 설정이 되어 있는 상태에서 git add * 을 하게 되면,
# git add * The following paths are ignored by one of your .gitignore files: upload Use -f if you really want to add them. fatal: no files added # git add .
위와 같은 에러가 뜨면서 안되니, .gitignore파일에 설정을 한 후에는 git add . 으로 add를 하기 바란다.

//---------- 추가 2018.01.25
그런데 .gitignore파일에 들어간 것들은 commit을 이미 한뒤에는 적용이 되질 않는다.
이미 commit을 한 파일도 무시를 하고 싶으면, 아래와 같은 명령어들을 사용하면 된다.
git update-index --assume-unchange 파일명
해당 파일이 바뀌어도 무시한다
git update-index --no-assume-unchange 파일명
다시 무시하지 않도록 목록에서 제외 시킨다
git ls-files -v | grep '^h'
무시된 파일 목록을 본다.

단, 위의 기능은 .gitignore파일에 저장되는 내용이 아니기 때문에, 해당 작업 장소마다 설정을 해주어야 하는 번거로움이 있다..
//----------

자 그럼 이제 git add를 했는데, 이게 잘 됐는지 궁금해진다. 그럴때는 git status를 쳐본다.
# git add test.php # git add readme.rst # git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached ..." to unstage) # # new file: readme.rst # new file: test.php # # Untracked files: # (use "git add ..." to include in what will be committed) . . .
위 결과를 보면 필자가 테스트로 add한 파일 2개가 new file: 형태로 제대로 add 된걸 볼 수 있다.

하지만 여전히 문제가 발생하곤 하는데, gitignore파일에 미처 설정해놓지 못했던 파일이 add 목록에 포함되어 있는걸 발견했거나 하는 경우이다.
이럴때는 git reset으로 add된 목록을 초기화 시킬수 있다.
# git reset # git status # On branch master # # Initial commit # # Untracked files: . . .
changes 목록이 초기화 된걸 볼 수 있다.

자 그럼 커밋을 해보자.
# git commit -m "test서버 최초 커밋" *** Please tell me who you are. Run git config --global user.email "you@example.com" git config --global user.name "Your Name" . . . # git config --global user.email "b1ix@abc.ef" # git config --global user.name "b1ix" # git commit -m "test서버 최초 커밋" create mode 100644 file1.html create mode 100644 file2.html create mode 100644 file3.html . . .
필자 같은 경우 커밋을 하는 user가 설정되어 있지 않아서, 이름과 email을 설정한 후에 커밋을 실행 했다.

자 이제 여기까지 진행 됐으면, remote저장소에 push를 해보자.
일단 아래와 같이 remote저장소를 설정한 후 git push를 해보겠다.
# git remote add origin http://b1ix.com:9000/test_server
# git push warning: push.default is unset; its implicit value is changing in Git 2.0 from 'matching' to 'simple'. To squelch this message and maintain the current behavior after the default changes, use: git config --global push.default matching To squelch this message and adopt the new behavior now, use: git config --global push.default simple See 'git help config' and search for 'push.default' for further information. (the 'simple' mode was introduced in Git 1.7.11. Use the similar mode 'current' instead of 'simple' if you sometimes use older versions of Git) Password for 'http://b1ix.com:9000': No refs in common and none specified; doing nothing. Perhaps you should specify a branch such as 'master'. Everything up-to-date # # # git config --global push.default matching # git config --global push.default simple # git push fatal: The current branch master has no upstream branch. To push the current branch and set the remote as upstream, use git push --set-upstream origin master # # # git push --set-upstream origin master Password for 'http://b1ix.com:9000': Counting objects: 2969, done. Compressing objects: 100% (2849/2849), done. Writing objects: 100% (2969/2969), 51.11 MiB | 7.75 MiB/s, done. Total 2969 (delta 889), reused 0 (delta 0) remote: Resolving deltas: 100% (889/889) remote: Updating references: 100% (1/1) To http://b1ix.com:9000/test_server * [new branch] master -> master Branch master set up to track remote branch master from origin.
몇몇 설정이 제대로 되어 있지 않는 바람에 push 명령어 후에 해당 설정을 해달라는 식으로 오류 메세지가 떴었다.
그리고 해당 설정을 전부 처리한 후에 git push를 하니 제대로 push가 이루어 진것을 볼 수 있다.


지금 필자가 해놓은 작업은 실제 서버로 쓰이는 test_server디렉토리에 git저장소를 설치한 것이다.
그렇다는 것은 실제로 개발을 하는 곳은 따로 있어야 한다는 것이다. 그럴때 필요한 기능이 git clone이다.
# git clone http://b1ix.com:9000/test_server Cloning into 'test_server'... remote: Counting objects: 2969, done remote: Finding sources: 100% (2969/2969) remote: Getting sizes: 100% (2079/2079) remote: Total 2969 (delta 889), reused 2969 (delta 889) Receiving objects: 100% (2969/2969), 51.11 MiB | 10.95 MiB/s, done. Resolving deltas: 100% (889/889), done. Checking out files: 100% (3665/3665), done.
개발을 할 환경으로 간뒤에, git clone remote저장소주소 식으로 명령을 내리면, 해당 디렉토리의 하위에 test_server라는 디렉토리가 생기고 그 아래 해당 소스가 전부 복사되어 있는것을 볼 수 있을 것이다.
이렇게 까지 되었다면, 개발환경에서 개발을 진행하면서, git push로 remote저장소에 수정사항을 psuh 한뒤, 실제 사용중인 test_server에서 git pull로 변경 사항을 가져오는 식으로 형상관리를 하면 될 것이다.