본문 바로가기

Git/Git & CodeCommit & Bitbucket

신규 Branch 생성 후 파일 업로드하기

# 신규 Branch 생성 후 파일 업로드 하기

1. 기본 명령어

# git init, 해당 폴더를 git에 등록
git init

# git remote add {remote_name} https://github.com/~.git
git remote add origin https://github.com/~.git

# git pull: 원격 저장소 정보를 가져오면서 local branch와 병합한다.
git pull origin 브런치이름

# Branch를 변경한다. -b 명령어는 브랜치가 없으면 생성한다.
git checkout -b 브런치이름

# 신규 파일을 추가한다.
git add .

# 신규 파일을 commit 한다.
git commit -m "어쩌구"

# git push {원격 Repository Name} {branch_name}
git push origin 브런치이름

* 해당 branch가 이미 생성되어야 할 수도 있으니 참고.

2. 테스트

AWS CodeCommit에 seungkim 이라는 브랜치가 존재

해당 브랜치로 변경 후 origin에서 commit 및 push하기

1. 신규 브랜치 생성
git checkout -b seungkim

2. git branch로 현재 branch를 확인한다.

3. git add. 명령어로 파일을 추가한다.

4. git commit -m "test"로 추가된 파일을 커밋한다.

5. git push origin seungkim 명령어를 실행하면 아래와 같은 에러 발생
 ! [rejected]        seungkim -> seungkim (non-fast-forward)
error: failed to push some refs to 'https://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/test
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

6. git push origin +seungkim 명령어로 강제로 push 하면 된다.
- 강제로 push를 하면 기존 브랜치의 파일들은 삭제되고 origin 브랜치에 존재하는 파일들이 업로드 된다.

- 신규 브랜치 생성 후 테스트

git checkout -b test 로 신규 브랜치를 생성

git push origin test 명령어로 신규 브랜치에 origin data 업로드

브랜치가 없으니까 신규로 생성하면서 origin data를 업로드

$ git push origin test
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To https://git-codecommit.ap-northeast-2.amazonaws.com/v1/repos/Jenkins-Test
 * [new branch]      test -> test

3. 원격 브랜치를 가져와서 신규 파일을 추가하고 신규 브랜치로 업로드 하는 방법

1. 체크아웃 할 수 있는 브랜치 목록 보기

git branch -a

- 결과

$ git branch -a
  codecommit
  kim
  master
* remote-branch
  seungkim
  test
  test_branch
  remotes/origin/codecommit
  remotes/origin/kim
  remotes/origin/main
  remotes/origin/master
  remotes/origin/remote-branch
  remotes/origin/seungkim
  remotes/origin/test
  remotes/origin/test_branch

2. 원격 브랜치의 변경 이력을 가져와서 병합하기

- 원격 브랜치에서는 작업을 할 수 없다. 변경 사항을 추가하기 위해서는 원격 브랜치의 복사본이 필요하고

해당 복사본에서 우리는 작업을 할 수 있다.

예를들어 위에있는 remotes/origin/kim 원격 브랜치를 복사하고 싶다면 아래의 명령어로 실행해야 한다.

git checkout -b branch-test-a remotes/origin/kim

위의 명령어를 실행하면 branch-test-a 라는 신규 브랜치가 생성이되고 remotes/origin/kim라는 원격 브랜치의 변경이력을 새 브랜치인 branch-test-a로 가져온다.

- 이제는 원하는 원격 브랜치의 복사복을 갖게 되었다. 이제 해당 원격 브랜치에 커밋과 푸쉬도 진행해보자.

3. 원격 브랜치로 커밋과 푸쉬하기

- 신규 파일을 생성해보자.

여기서는 branch-test.txt 파일을 신규로 생성하고 commit과 push를 진행한다.

그럼 원격 브랜치 복사본인 remotes/origin/kim의 내용과 여기서 추가한 branch-test.txt 파일이 추가되어 새로운 브랜치인 branch-test-a 가 신규로 원격 저장소에 파일들과 함께 생성이 될것이다.

- 실습을 진행해보자.

# 신규 파일을 생성한다. 내용은 아무거나 입력한다.
$ vi branch-test.txt

# 생성된 파일목록 확인
$ ls -l
branch-test.txt

# 생성된 파일을 추가한다.
git add .

# 생성된 파일을 커밋한다.
git commit -m "add new file"

# 생성된 파일과 함께 원격 브랜치에 push 한다.
git push origin branch-test-a

4. 결과

- 위의 결과를 보면 아래와 같다.

[new branch] 라고 나오는것은 신규로 브랜치를 생성하면서 기존에 가져온 remotes/origin/kim의 내용과 신규로 생성한 branch-test.txt을 추가하여 브랜치를 새로 만들었다고 이해하면 된다.

- AWS CodeCommit 브랜치를 보면 아래와 같이 나온다.

폴더 리스트, 폴더 리스트에 보면 신규로 추가한 test_branch 폴더를 확인 할 수 있다.

test_branch 폴더를 제외하면 나머지는 기존의 remotes/origin/kim의 리소스를 그대로 가져온것을 볼 있다.

한마디로 기존의 remotes/origin/kim의 브랜치 내용에서 test_branch폴더가 추가 되었다고 보면 된다.

파일을 보면 아래와 같다.

branch-test-a라는 브랜치가 신규로 생성이 되었고, 해당 브랜치 안에 test_branch 폴더에 있는 branch-test.txt가 신규로 생성되어 업로드되어 있다.

 

- 끝 -