git - How to modify existing, unpushed commits? -


i wrote wrong thing in commit message. alternatively, i've forgotten include files.

how can change commit message/files? commit has not been pushed yet.

amending recent commit message

git commit --amend 

will open editor, allowing change commit message of recent commit. additionally, can set commit message directly in command line with:

git commit --amend -m "new commit message" 

…however, can make multi-line commit messages or small corrections more cumbersome enter.

make sure don't have working copy changes staged before doing or committed too. (unstaged changes not committed.)

changing message of commit you've pushed remote branch

if you've pushed commit remote branch, you'll need force push commit with:

git push <remote> <branch> --force # or git push <remote> <branch> -f 

warning: force-pushing overwrite remote branch state of local one. if there commits on remote branch don't have in local branch, will lose commits.

warning: cautious amending commits have shared other people. amending commits rewrites them have different sha ids, poses problem if other people have copies of old commit you've rewritten. has copy of old commit need synchronize work newly re-written commit, can difficult, make sure coordinate others when attempting rewrite shared commit history, or avoid rewriting shared commits altogether.


use interactive rebase

another option use interactive rebase.
allows edit message want update if it's not latest message.

in order git squash, follow these steps:

// x number of commits last commit want able edit git rebase -i head~x 

once squash commits - choose e/r editing message

enter image description here

important note interactive rebase

when use git rebase -i head~x there can more x commits. git "collect" commits in last x commits , if there merge somewhere in between range see commits outcome x+.

good tip:

if have more single branch , might face conflicts when amending content set git rerere , let git resolve conflicts automatically you.


documentation


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -