When you have a renamed file and changed file which should not be added to staging.
git add -A filename
This command will add both the renamed file and old file info.
When you have a deleted file and need to add the file to staging.
git add -u filename
When you want to avoid adding staging some parts in a file,
First, see the changed part
git diff filename
Then,
git add -p filename
s
You can split the changed part
git diff –cached filename
When you want to make a change to pushed commit, first you need to check the commit nubmer by
git reflog, and then, you need to git revert git revert commitnumber
Then, you can check the file name and reverted part by
git diff HEAD~1
You can check diff between commits
git diff HEAD~1..HEAD git diff HEAD~2..HEAD
You can see the working tree
git log --oneline --graph --decorate --oneline
Git rebase
Edit the commit message the past commit made older than three
Example git commit is like this
0397116 Modified the first line 783e423 Edited new file again 0e6616c Edited new file a8d305e Made a new file
so if you git rebase 0e6616c Edited new file
git rebase -i a8d305e
This shows the commit after 0e6616c without including the commit itself. You may notice that the order of the commit is also from older to newer.
pick 0e6616c Edited new file pick 783e423 Edited new file again pick 0397116 Modified the first line # Rebase 0e6616c..0397116 onto 0e6616c # # 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 # # If you remove a line here THAT COMMIT WILL BE LOST. # However, if you remove everything, the rebase will be aborted.
To change 0e6616c commit message, you need to change ‘pick’ to ‘reward'(shorthand: ‘r’), and overwrite it.
r 0e6616c Edited new file pick 783e423 Edited new file again pick 0397116 Modified the first line
Then, editor will open immediately, so edit the commit message, and save it.
Edited the commit message # Please enter the commit message for your changes. Lines starting # with '#' will be ignored, and an empty message aborts the commit. # Not currently on any branch. # Changes to be committed: # (use "git reset HEAD^1 ..." to unstage) # # modified: newfile.txt
In case you choose edit mode, you can continue git rebase by git rebase –continue.
Merge a branch without leaving history of the branch using Git rebase
You can merge a branch into another branch without leaving its history using git rebase.
We have the history below on develop branch.
83cc4a4 Third new line on develop d841441 Another new line on develop 958766b New line on develop 0b4b08b Modified the first line 3160d83 Edited new file again e48e1ec Edited the commit message a8d305e Made a new file
And, the new branch branched off develop has this history.
f264ad7 Modified the line 4d4c182 New line of the second file 3bf6232 Added a new file 0b4b08b Modified the first line 3160d83 Edited new file again e48e1ec Edited the commit message a8d305e Made a new file
Then, a newbranch should be merged into develop branch without making newbranch history.
git rebase develop
Because the same file is edited, you encounter conflict so resolve the conflict. If it is successful, then you will see
First, rewinding head to replay your work on top of it...Applying: Added a new file Applying: New line of the second file Applying: Modified the line
You need to fast-forward merge which only moves HEAD position.
git checkout develop git merge newbranch
Now develop branch has the history below. (You will not see any merged branch)
24a927e Modified the line 3f9e302 New line of the second file e23361b Added a new file 83cc4a4 Third new line on develop d841441 Another new line on develop 958766b New line on develop 0b4b08b Modified the first line 3160d83 Edited new file again e48e1ec Edited the commit message a8d305e Made a new file 3f9e302 New line of the second file
Because parent commit is different, the added commits have new id numbers.
Merge a branch branched off another branch into develop using Git rebase –onto
thirdbranch has this history. thirdbranch branched off “Modified the line” of the newbranch. (So the two branch have “Modified the line” commit)
9d209a8 Added one more line on newfile3 7826f31 Added another line on newfile3 9c738c3 Added one more file 24a927e Modified the line
…(same commit with newbranch)
Then, if you want to merge from “Added one more file” and the rest of the commit into develop branch without including “Modified the line” commit which newbranch also has, then you should do,
git rebase --onto develop newbranch thirdbranch
Then, you should fast-forward marge
git checkout develop git merge thirdbranch
*Note: if newbranch doesn’t have a new commit from “Modified the line”, it just says
Already on ‘thirdbranch’
Current branch thirdbranch is up to date.
For more information, you should check out https://git-scm.com/book/en/v2/Git-Branching-Rebasing
コメント