git & github 공부하기 - Tirrilee/TechTalk GitHub Wiki

개행 문자 Warning

Start.txt 파일을 vi 명령어로 사용해서 만든 후 add 를 진행했는데 아래와 같은 에러가 발생하였다.

warning: LF will be replaced by CRLF in README.txt.

The file will have its original line endings in your working directory.

리눅스와 유닉스에서는 LF로 개행문자를 사용하고

윈도우는 CRLF 형식으로 개행문자를 사용한다.

만약 OS가 다른 상태에서 작업하면 이 문제는 힘들게 다가올 수 있다.

$ git config core.autocrlf true 을 사용하면 깔끔하다.


branch 생성 시

repository에 아무것도 없으면 git branch new-branch-namefatal: Not a valid object name: 'master'. 에러가 나타난다.

repository를 만들면 에러가 해결된다.


commit 삭제하기

참고) Git 되돌리기

$ git log 로 commit id 확인

$ git reset --hard commit-id : 특정 commit-id로 이동하고 commit은 삭제 & 해당 commit 상태로 폴더와 파일 상태가 돌아감

이후 서버에도 적용하기 위해 $ git push origin +branch-name 을 사용!!

  • --hard
  • --soft
  • --mixed

git branch 이름 변경하기

$ git branch -m 변경전_branch_name 새로운_branch_name

변경전 branch로 checkout 후

$ git push origin :old_branch 를 진행하면 이전 branch가 삭제된다.

왜 저 명령어가 이전 branch를 삭제하는 것이냐면 원래

$ git push local-branch:github-branch 이렇게 하면 local에서 만든 새로운 branchgithub의 github-branch에 올리겠다 는 의미이다.

즉 이를 바탕으로 위의 명령어는 local에서 존재하지 않는 branch 즉 null과 같은 branch를 github의 old_branch에 push하겠다는 의미 이므로

github의 old_branch를 삭제한다는 의미이다.

아직 local에서는 branch가 삭제되지 않았다.

현재 삭제할 branch에 있다면

$ git checkout another_branch_name 을 하여 branch를 변경하고

$ git branch -d branch_name 을 하여 branch를 삭제한다.


rebase & merge


rebase

$ git checkout master

$ git rebase A_branch : master 뒤에 A_branch의 commit들을 넣는다. 하지만 master의 head는 그대로 이다.

또는

$ git rebase master A_branch

$ git merge A_branch : master의 head를 A_branch가 가리키는 head로 이동시킨다.

$ git add .

$ git commit -m "message"

$ git push origin master


merge

$ git checkout master

$ git merge A_branch : mater에서 A_branch를 땡겨서 merge한다.

$ git add .

$ git commit -m "message"

$ git push origin master

  • merge : 새로 만들어진 commit의 parent는 merge된 branch의 마지막 commit & 현재 branch의 이전 commit이다. 모든 히스토리가 남으며, 새로운 commit이 생성되는 것이 특징이다.
  • squash and merge : 새로 commit이 만들어지고, 그 commit에 merge 된 branch의 모든 commit이 남겨진다. 즉 새로운 commit은 이전 commit 만이 parent이며 다른 parent는 존재하지 않는다.
  • rebase and merge : 새로운 commit이 만들어지지 않고, 기존의 branch에 merge할 branch의 commit이 덧붙여진다.

+) reword : 커밋 메시지 변경 가능 / squash : 커밋을 하나로 묶음

⚠️ **GitHub.com Fallback** ⚠️