- The changes that are committed are those that have been added to the index (staged changes)
- Frequently rebase your feature branch to make process of resolving conflict easier in the future. (diverged commits between the main and the feature branches)
git branch -m master main
git commit --allow-empty -m "Upgrading to heroku-22"
git log --pretty=oneline git log --oneline
https://stackoverflow.com/questions/1085162/commit-only-part-of-a-file-in-git
git add --patch git add -p
git remote -v git remote -v origin
git remote add origin git-url-address.git
git remote set-url origin git-url-address.git
git checkout -b newbranch origin/xbranch
(new local branch has different name with tracked remote branch)
git checkout --track origin/xbranch
(new local branch has the same name with tracked remote branch)
git checkout git push -u origin
or
git checkout git push -u origin HEAD
you can use a commit hash
git checkout a1b2c3
you can use a tag name
git checkout v2.0.0
you can use a tag name either with /tags prefix
git checkout tags/v2.0.0
now we are in a state known as "detached HEAD", don't make any new commit here
git checkout main
git config --global alias.amend 'commit --amend --no-edit' git config --global alias.line 'log --oneline' git config --global alias.pretty 'log --pretty=oneline'
Then, use like below:
git amend
git config --global core.editor "code --wait"
git commit --amend (vscode will be opened, make the changes and approve it, then...) git push --force-with-lease
Let's assume there are A, B, C, and D commits.
A - B - C - D <- HEAD --- A - B - C - D <- INDEX
Soft: If you would want to B, C, and D are in one commit
git reset --soft Ref-A
git reset --soft HEAD~3 (in this example) A <- HEAD --- A - (B - C - D) <- INDEX
Soft reset pretends to the changes are staged (index matches D) and you haven't did commit, so you can:
git commit -m "new commit message" A - E <- HEAD --- A - E <- INDEX
Mixed: If you would want to B, C, and D are in two or more commits
git reset --mixed Ref-A it is default, and same with (> git reset Ref-A)
git reset --mixed HEAD3 (in this example) git reset --mixed HEAD1 (last commit) A <- HEAD --- A <- INDEX but changes in B, C and D are still as it is
Mixed reset pretends to the changes are even not staged (index matches HEAD) and you haven't did commit, so you can:
git add
andgit commit
in a different grouping, either one, two or more
Hard: If you would want to B, C, and D are removed completely with all changes
git reset --hard Ref-A
git reset --hard HEAD3 (in this example)1 (last commit) A <- HEAD --- A <- INDEX all changes in B, C, D are completely removed
git reset --hard HEAD
Hard reset pretends to you did not any changes after A.
Hard: If you would want to B, C, and D are in different branch stemming from A
git branch
then now, you can do hard reset
git reset --hard Ref-A A <- HEAD --- A <- INDEX all changes in B, C, D are completely removed but the commits A,B,C,D are in the You should always rungit status
before doing a hard reset to make sure your working directory is clean or that you're okay with losing your uncommitted changes. Anyway, you can recover any committed changes with the reflog; uncommitted changes that are removed with reset --hard are gone forever.
As a Summary
--soft : HEAD moves to the Ref, changes are left staged (in index).
--mixed : HEAD moves to the Ref, unstage changes (extracted from index), changes are left in working tree.
--hard : HEAD moves to the Ref, unstage changes (extracted from index), delete the changes as well as any uncommitted changes
https://linuxize.com/post/change-git-commit-message/
Before changing the commit message, you can also add other changes
git add . git commit --amend -m "New commit message." Amends a commit without changing its commit message git commit --amend --no-edit If it is pushed, force push to update the history of the remote repository git push --force git push --force
N is the number of commits to perform a rebase on
git rebase -i HEAD~5
this displays the latest X commits in your default editor
Move to the lines of the commit message you want to change and replace pick with reword
Save the changes and close the editor
For each chosen commit, a new text editor window will open. Change the commit message, save the file, and close the editor.
Force push the changes to the remote repository
git push --force
git fetch origin
git diff main origin/main
git cherry main origin/main git cherry origin/main main
A tag is used to label and mark a specific commit in the history. https://git-scm.com/book/en/v2/Git-Basics-Tagging https://stackoverflow.com/questions/35979642/what-is-git-tag-how-to-create-tags-how-to-checkout-git-remote-tags
git tag
git tag -a tag_name -m "message"
git tag -a v1.0 -m "message" 9fceb02
git tag v12.0
By default, the git push command doesn’t transfer tags to remote servers, so needs --tags flag
git push origin v1.5 // a specifig tag
git push origin --tags //all tags
git tag -d v1.0 Note that this does not remove the tag from any remote servers.
git push origin --delete v.1.0
git fetch --all --tags
https://stevenmortimer.com/5-steps-to-change-github-default-branch-from-master-to-main/
git branch -m master main
git push -u origin main
git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main
git push origin --delete master
https://dev.to/ademola_isr/how-to-gitignore-untrack-files-already-pushed-to-github-repository-1h96
git add . git commit -m "message"
or
git status to see the branch is up to date with origin
git rm -r --cached > adding -cached allow us to remove the file from the index. Our files are still present
git add . git commit -m "gitignore fix" git push
git checkout . Don’t forget the final ‘.’ — You aren’t required to add this, and it may look like it has worked but if you leave this off it will take you to a new “detached head state” where you can make changes and it will allow you to make commits, but nothing will be saved and any commits you make will be lost.
git commit if you write the commit command without -m, a vim editor is opened.
For escape from vim without saving: tap i (input mode) and then ESC For escape from vim with saving: write this three char -> :wq
:w to write to the file and
:q to quit. Note: things in <> denote key presses.