Programing

Git에서 이전 커밋의 타임 스탬프를 어떻게 변경할 수 있습니까?

lottogame 2020. 10. 2. 21:22
반응형

Git에서 이전 커밋의 타임 스탬프를 어떻게 변경할 수 있습니까?


푸시되지 않은 기존 커밋을 수정하는 방법에 대한 답변 은 무엇입니까? 아직 업스트림으로 푸시되지 않은 이전 커밋 메시지를 수정하는 방법을 설명합니다. 새 메시지는 원래 커밋의 타임 스탬프를 상속합니다. 이것은 논리적으로 보이지만 시간을 재설정하는 방법이 있습니까?


사용 git filter-branchENV 필터와 함께 세트는 것을 GIT_AUTHOR_DATEGIT_COMMITTER_DATE커밋의 특정 해시 당신은 수정을 찾고 있습니다.

이것은 그와 미래의 모든 해시를 무효화합니다.

예:

commit 날짜 를 변경하려면 119f9ecf58069b265ab22f1f97d2b648faf932e0다음과 같이 할 수 있습니다.

git filter-branch --env-filter \
    'if [ $GIT_COMMIT = 119f9ecf58069b265ab22f1f97d2b648faf932e0 ]
     then
         export GIT_AUTHOR_DATE="Fri Jan 2 21:38:53 2009 -0800"
         export GIT_COMMITTER_DATE="Sat May 19 01:01:01 2007 -0700"
     fi'

대화 형 리베이스를 수행하고 날짜를 변경하려는 커밋에 대해 편집선택할 있습니다. 예를 들어 입력 한 커밋을 수정하기 위해 리베이스 프로세스가 중지되면 다음을 수행합니다.

git commit --amend --date="Wed Feb 16 14:00 2011 +0100"

그 후 대화식 리베이스를 계속합니다.

UPDATE (studgeek의 의견에 대한 응답으로) : 작성자 날짜 대신 커밋 날짜를 변경하려면 :

GIT_COMMITTER_DATE="Wed Feb 16 14:00 2011 +0100" git commit --amend

위의 행은 커밋 수정에 사용되는 환경 변수 GIT_COMMITTER_DATE를 설정합니다.

모든 것은 Git Bash에서 테스트됩니다.


이러한 모든 제안을 하나의 명령으로 처리하는 더 좋은 방법은

LC_ALL=C GIT_COMMITTER_DATE="$(date)" git commit --amend --no-edit --date "$(date)"

그러면 마지막 커밋의 커밋과 작성자 날짜가 "지금"으로 설정됩니다.


그냥하세요 git commit --amend --reset-author --no-edit. 이전 커밋의 경우 대화 형 리베이스를 수행하고 edit날짜를 수정할 커밋을 선택할 수 있습니다.

git rebase -i <ref>

그런 다음 --reset-author--no-edit커밋을 수정하여 작성자 날짜를 현재 날짜로 변경합니다.

git commit --amend --reset-author --no-edit

마지막으로 대화식 리베이스를 계속하십시오.

git rebase --continue

나는 이것을 위해 스크립트와 Homebrew 패키지를 작성했습니다. 설치가 매우 쉬우 며 GitHub PotatoLabs/git-redate페이지 에서 찾을 수 있습니다 .

통사론:

git redate -c 3

실행하기 만하면 git redate가장 최근 5 개 커밋의 vim에서 모든 날짜를 편집 할 수 있습니다 ( -c되돌리고 싶은 커밋 수에 대한 옵션 도 있으며 기본값은 5입니다). 질문, 의견 또는 제안 사항이 있으면 알려주세요!

여기에 이미지 설명 입력


각 커밋은 커미터 날짜와 작성자 날짜의 두 날짜와 연결됩니다. 다음과 같은 날짜를 볼 수 있습니다.

git log --format=fuller

마지막 6 개 커밋의 작성자 날짜와 커미터 날짜를 변경하려면 대화 형 rebase를 사용하면됩니다.

git rebase -i HEAD~6

.

pick c95a4b7 Modification 1
pick 1bc0b44 Modification 2
pick de19ad3 Modification 3
pick c110e7e Modification 4
pick 342256c Modification 5
pick 5108205 Modification 6

# Rebase eadedca..5108205 onto eadedca (6 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit

For all commits where you want to change the date, replace pick by edit (or just e), then save and quit your editor.

You can now amend each commit by specifying the author date and the committer date in ISO-8601 format:

GIT_COMMITTER_DATE="2017-10-08T09:51:07" git commit --amend --date="2017-10-08T09:51:07"

The first date is the commit date, the second one is the author date.

Then go to the next commit with :

git rebase --continue

Repeat the process until you amend all your commits. Check your progression with git status.


Building on theosp's answer, I wrote a script called git-cdc (for change date commit) that I put in my PATH.

The name is important: git-xxx anywhere in your PATH allows you to type:

git xxx
# here
git cdc ... 

That script is in bash, even on Windows (since Git will be calling it from its msys environment)

#!/bin/bash
# commit
# date YYYY-mm-dd HH:MM:SS

commit="$1" datecal="$2"
temp_branch="temp-rebasing-branch"
current_branch="$(git rev-parse --abbrev-ref HEAD)"

date_timestamp=$(date -d "$datecal" +%s)
date_r=$(date -R -d "$datecal")

if [[ -z "$commit" ]]; then
    exit 0
fi

git checkout -b "$temp_branch" "$commit"
GIT_COMMITTER_DATE="$date_timestamp" GIT_AUTHOR_DATE="$date_timestamp" git commit --amend --no-edit --date "$date_r"
git checkout "$current_branch"
git rebase  --autostash --committer-date-is-author-date "$commit" --onto "$temp_branch"
git branch -d "$temp_branch"

With that, you can type:

git cdc @~ "2014-07-04 20:32:45"

That would reset author/commit date of the commit before HEAD (@~) to the specified date.

git cdc @~ "2 days ago"

That would reset author/commit date of the commit before HEAD (@~) to the same hour, but 2 days ago.


Ilya Semenov mentions in the comments:

For OS X you may also install GNU coreutils (brew install coreutils), add it to PATH (PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH") and then use "2 days ago" syntax.


git commit --amend --date="now"

if it is previous last commit.

git rebase  -i HEAD~2
git commit --amend --date=now

if you already push to orgin and can force use:

git push --force 

if you can't force the push and if it is pushed, you can't change the commit! .


Here is a convenient alias that changes both commit and author times of the last commit to a time accepted by date --date:

[alias]
    cd = "!d=\"$(date -d \"$1\")\" && shift && GIT_COMMITTER_DATE=\"$d\" \
            git commit --amend --date \"$d\""

Usage: git cd <date_arg>

Examples:

git cd now  # update the last commit time to current time
git cd '1 hour ago'  # set time to 1 hour ago

Edit: Here is a more-automated version which checks that the index is clean (no uncommitted changes) and reuses the last commit message, or fails otherwise (fool-proof):

[alias]
    cd = "!d=\"$(date -d \"$1\")\" && shift && \
        git diff-index --cached --quiet HEAD --ignore-submodules -- && \
        GIT_COMMITTER_DATE=\"$d\" git commit --amend -C HEAD --date \"$d\"" \
        || echo >&2 "error: date change failed: index not clean!"

I created this npm package to change date of old commits.

https://github.com/bitriddler/git-change-date

Sample Usage:

npm install -g git-change-date
cd [your-directory]
git-change-date

You will be prompted to choose the commit you want to modify then to enter the new date.

If you want to change a commit by specific hash run this git-change-date --hash=[hash]


The following bash function will change the time of any commit on the current branch.

Be careful not to use if you already pushed the commit or if you use the commit in another branch.

# rewrite_commit_date(commit, date_timestamp)
#
# !! Commit has to be on the current branch, and only on the current branch !!
# 
# Usage example:
#
# 1. Set commit 0c935403 date to now:
#
#   rewrite_commit_date 0c935403
#
# 2. Set commit 0c935403 date to 1402221655:
#
#   rewrite_commit_date 0c935403 1402221655
#
rewrite_commit_date () {
    local commit="$1" date_timestamp="$2"
    local date temp_branch="temp-rebasing-branch"
    local current_branch="$(git rev-parse --abbrev-ref HEAD)"

    if [[ -z "$date_timestamp" ]]; then
        date="$(date -R)"
    else
        date="$(date -R --date "@$date_timestamp")"
    fi

    git checkout -b "$temp_branch" "$commit"
    GIT_COMMITTER_DATE="$date" git commit --amend --date "$date"
    git checkout "$current_branch"
    git rebase "$commit" --onto "$temp_branch"
    git branch -d "$temp_branch"
}

To change both the author date and the commit date:

GIT_COMMITTER_DATE="Wed Sep 23 9:40 2015 +0200" git commit --amend --date "Wed Sep 23 9:40 2015 +0200"

If you want to get the exact date of another commit (say you rebase edited a commit and want it to have the date of the original pre-rebase version):

git commit --amend --date="$(git show -s --format=%ai a383243)"

This corrects the date of the HEAD commit to be exactly the date of commit a383243 (include more digits if there are ambiguities). It will also pop up an editor window so you can edit the commit message.

That's for the author date which is what you care for usually - see other answers for the committer date.


If you want to perform the accepted answer (https://stackoverflow.com/a/454750/72809) in standard Windows command line, you need the following command:

git filter-branch -f --env-filter "if [ $GIT_COMMIT = 578e6a450ff5318981367fe1f6f2390ce60ee045 ]; then export GIT_AUTHOR_DATE='2009-10-16T16:00+03:00'; export GIT_COMMITTER_DATE=$GIT_AUTHOR_DATE; fi"

Notes:

  • It may be possible to split the command over multiple lines (Windows supports line splitting with the carret symbol ^), but I didn't succeed.
  • You can write ISO dates, saving a lot of time finding the right day-of-week and general frustration over the order of elements.
  • If you want the Author and Committer date to be the same, you can just reference the previously set variable.

Many thanks go to a blog post by Colin Svingen. Even though his code didn't work for me, it helped me find the correct solution.


There are already many great answers, but when I want to change date for multiple commits in one day or in one month, I don't find a proper answer. So I create a new script for this with explaintion, hope it will help someone:

#!/bin/bash

# change GIT_AUTHOR_DATE for commit at Thu Sep 14 13:39:41 2017 +0800
# you can change the data_match to change all commits at any date, one day or one month
# you can also do the same for GIT_COMMITTER_DATE

git filter-branch --force --env-filter '

date_match="^Thu, 14 Sep 2017 13+"              

# GIT_AUTHOR_DATE will be @1505367581 +0800, Git internal format 
author_data=$GIT_AUTHOR_DATE;                   
author_data=${author_data#@}                  
author_data=${author_data% +0800}                # author_data is 1505367581     

oneday=$((24*60*60))

# author_data_str will be "Thu, 14 Sep 2017 13:39:41 +0800", RFC2822 format
author_data_str=`date -R -d @$author_data`      

if [[ $author_data_str =~ $date_match ]];
then
    # remove one day from author_data
    new_data_sec=$(($author_data-$oneday))
    # change to git internal format based on new_data_sec
    new_data="@$new_data_sec +0800"             
    export GIT_AUTHOR_DATE="$new_data"
fi
' --tag-name-filter cat -- --branches --tags

The date will be changed:

AuthorDate: Wed Sep 13 13:39:41 2017 +0800

If commit not yet pushed then I can use something like that: git commit --amend --date=" Wed Mar 25 10:05:44 2020 +0300" after that git bash opens editor with the already applied date so you need just to save it by typing in the VI editor command mode ":wq" and you can push it

참고 URL : https://stackoverflow.com/questions/454734/how-can-one-change-the-timestamp-of-an-old-commit-in-git

반응형