ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 나를 살려준 깃 커맨드, git reflog
    끄적끄적 2024. 2. 27. 20:54

    레퍼런스

    Git - git-reflog Documentation

     

    Git - git-reflog Documentation

    git-reflog - Manage reflog information

    git-scm.com

    상황

    오늘 작업한 작업물을 리베이스하는 도중에 예상치 못한 packages 관련된 충돌이 발생하였다. 따라서 기존 브랜치에서 conflict을 해결하고 리베이스하기 위해 해당 리베이스를 abort했다.

     

    문제는 브랜치의 origin에 푸쉬하지 않은 local commit이 지워졌는데, 동료의 도움으로 git reflog 를 통해 회생시킬 수 있었다.

    $ git reflog
    

    reflog에 대한 가벼운 설명

    This command manages the information recorded in the reflogs.

    Reference logs, or "reflogs", record when the tips of branches and other references were updated in the local repository. Reflogs are useful in various Git commands, to specify the old value of a reference. For example, HEAD@{2} means "where HEAD used to be two moves ago", master@{one.week.ago} means "where master used to point to one week ago in this local repository", and so on. See gitrevisions[7] for more details.

     

    The command takes various subcommands, and different options depending on the subcommand:

    The "show" subcommand (which is also the default, in the absence of any subcommands) shows the log of the reference provided in the command-line (or HEAD, by default). The reflog covers all recent actions, and in addition the HEAD reflog records branch switching.

     

    The "expire" subcommand prunes older reflog entries. Entries older than expire time, or entries older than expire-unreachable time and not reachable from the current tip, are removed from the reflog. This is typically not used directly by end users — instead, see git-gc[1].

     

    The "delete" subcommand deletes single entries from the reflog. Its argument must be an exact entry (e.g. "git reflog delete master@{2}"). This subcommand is also typically not used directly by end users.

     

    The "exists" subcommand checks whether a ref has a reflog. It exits with zero status if the reflog exists, and non-zero status if it does not.


    요약하자면, git reflog 명령어를 통해, 로컬이라도 푸쉬했던 커밋들이 아직 캐싱 레이어에 남아있어서 사라진 커밋들에 대한 기록을 볼 수 있다.

     

    내가 필요한 건 81xx HEAD@{31}: (refactor): Divide the component 리베이스에 실패한 커밋이었고, 해당 커밋으로 이동할 수 있었다.

     

     

    $ git checkout [타깃 HEAD의 커밋 이름]
    >>>
    $ git checkout 81979dda8
    

    체크아웃을 하기 전에, 커밋에 올라가기 전 파일이 유실될 수 있으므로 stash를 이용하도록 하자.

     

    체크아웃을 하게 되면, HEAD로 하여금 이전 기록이 포함된 코드를 다시 회생시킨다. 해당 체크아웃은 origin 또는 local에 브랜치를 만들지 않으므로, 해당 commit history를 남기고 싶다면, 해당 commit 들의 최신 Head 또는 원하는 Head에 branch를 만들어야 한다.

     

    임시 커밋이므로 나는 이름을 hey로 지었다.

     

     

    이후 마지막 커밋이었던 친구 (refactor): Divide the component 를 cherry pick으로 나의 기존 커밋의 최신 부분에 가져왔다.

     

    결론

    너희들은 다 계획이 있구나 ? 리베이스 해야 할 때는 stash 보단 최소한 로컬에라도 커밋을 푸쉬해두기.

    git 만세

    댓글

Designed by Tistory.