|
@@ -308,20 +308,25 @@ git log --diff-filter=D --summary | grep delete
|
|
|
|
|
|
# Git commands that can help resolve merge conflicts
|
|
|
## General tools:
|
|
|
-git status % The status command is in frequent use when a working with Git and during
|
|
|
- a merge it will help identify conflicted files.
|
|
|
-git log --merge % Passing the --merge argument to the git log command will produce a log
|
|
|
- with a list of commits that conflict between the merging branches.
|
|
|
-git diff % diff helps find differences between states of a repository/files. This
|
|
|
- is useful in predicting and preventing merge conflicts.
|
|
|
+git status % The status command is in frequent use when a working with Git and
|
|
|
+ during a merge it will help identify conflicted files.
|
|
|
+git log --merge % Passing the --merge argument to the git log command will produce a
|
|
|
+ log with a list of commits that conflict between the merging branches.
|
|
|
+git diff % diff helps find differences between states of a repository/files.
|
|
|
+ This is useful in predicting and preventing merge conflicts.
|
|
|
+
|
|
|
## Tools for when git fails to start a merge
|
|
|
-git checkout % checkout can be used for undoing changes to files, or for changing branches
|
|
|
-git reset --mixed % reset can be used to undo changes to the working directory and staging
|
|
|
+git checkout % checkout can be used for undoing changes to files,
|
|
|
+ or for changing branches
|
|
|
+git reset --mixed % reset can be used to undo changes to the working directory
|
|
|
+ and staging
|
|
|
+
|
|
|
## Tools for when git conflicts arise during a merge
|
|
|
-git merge --abort % Executing git merge with the --abort option will exit from the merge
|
|
|
- process and return the branch to the state before the merge began.
|
|
|
-git reset % Git reset can be used during a merge conflict to reset conflicted files
|
|
|
- to a know good state
|
|
|
+git merge --abort % Executing git merge with the --abort option will exit from the
|
|
|
+ merge process and return the branch to the state before the merge
|
|
|
+ began.
|
|
|
+git reset % Git reset can be used during a merge conflict to reset conflicted
|
|
|
+ files to a know good state
|
|
|
|
|
|
# gitlab reference
|
|
|
https://gitlab.com/pages/plain-html
|
|
@@ -770,3 +775,121 @@ The 7 rules of a great commit message
|
|
|
5. Use the imperative mood in the subject line
|
|
|
6. Wrap the body at 72 characters
|
|
|
7. Use the body to explain what and why vs. how
|
|
|
+
|
|
|
+# merge develop branch into main
|
|
|
+To merge develop branch into the main branch - you checkout main and merge develop:
|
|
|
+$ git checkout develop
|
|
|
+# ...develop some code...
|
|
|
+$ git add .
|
|
|
+$ git commit -m 'some commit message'
|
|
|
+$ git checkout main
|
|
|
+# ...switched to branch main...
|
|
|
+$ git merge develop
|
|
|
+Reference: https://stackabuse.com/git-merge-branch-into-master/
|
|
|
+
|
|
|
+# undo latest local commit
|
|
|
+Let's create a file we'd like to add to our repository, add it and finally commit:
|
|
|
+
|
|
|
+$ echo "Hello World!" >> file.txt
|
|
|
+$ git add file.txt
|
|
|
+$ git commit -m 'added file.txt'
|
|
|
+$ echo "It's a great day today :(" >> file.txt
|
|
|
+$ git add file.txt
|
|
|
+$ git commit -m 'modified file.txt'
|
|
|
+
|
|
|
+Yikes, we've accidentally left in a horrible typo in the content. We've put in
|
|
|
+the parentheses in the wrong way! And we've just committed that mistake:
|
|
|
+
|
|
|
+$ git log --pretty=oneline
|
|
|
+df9fc1b773ecf3ba14c990615831f1087817611f (HEAD -> main) Modified file.txt
|
|
|
+55db4f399d1ad64e0a40e1858d23fef0ffe31fb0 Added file.txt
|
|
|
+
|
|
|
+To remove a local commit, assuming it hasn't been pushed to the remote repository yet,
|
|
|
+we can use the `git reset` command, which is effectively the opposite of `git add`:
|
|
|
+
|
|
|
+$ git reset HEAD~
|
|
|
+Unstaged changes after reset:
|
|
|
+M file.txt
|
|
|
+
|
|
|
+We've reset the HEAD (pointer to the last commit), pointing it back (~) to the
|
|
|
+previous commit. By including a number after the tilde, we could've gone back
|
|
|
+multiple commits instead of just one.
|
|
|
+
|
|
|
+Once reset, the change we've made to the file.txt, i.e. the erroneous modification is
|
|
|
+unstaged. Let's take a look at the log again:
|
|
|
+
|
|
|
+$ git log --pretty=oneline
|
|
|
+55db4f399d1ad64e0a40e1858d23fef0ffe31fb0 (HEAD -> main) Added file.txt
|
|
|
+
|
|
|
+And the status is:
|
|
|
+
|
|
|
+$ git status
|
|
|
+On branch main
|
|
|
+Changes not staged for commit:
|
|
|
+ (use "git add <file>..." to update what will be committed)
|
|
|
+ (use "git restore <file>..." to discard changes in working directory)
|
|
|
+ modified: file.txt
|
|
|
+
|
|
|
+no changes added to commit (use "git add" and/or "git commit -a")
|
|
|
+
|
|
|
+However, what are the contents of the file now?
|
|
|
+
|
|
|
+$ nano file.txt
|
|
|
+
|
|
|
+Hello World
|
|
|
+It's a great day today :(
|
|
|
+
|
|
|
+By default, the reset command is --soft. The --soft flag doesn't reset the changes
|
|
|
+done to the file, just removes the commit that was made. Let's go ahead and re-commit
|
|
|
+this mistake again, so we can take a look at what happens when we run the --hard
|
|
|
+option.
|
|
|
+
|
|
|
+Hard Reset:
|
|
|
+Instead of the --soft reset, which we can use to simply undo the commit, while leaving
|
|
|
+the files intact (and the changes still present, which is why the git status command
|
|
|
+prompted us with staging the changes) - we can also do a --hard reset:
|
|
|
+
|
|
|
+$ git reset --hard HEAD~
|
|
|
+HEAD is now at 55db4f3 Added file.txt
|
|
|
+
|
|
|
+This time around, the changes aren't unstaged, like before. They're removed. If we
|
|
|
+check the log, it'll look much like last time:
|
|
|
+
|
|
|
+$ git log --pretty=oneline
|
|
|
+55db4f399d1ad64e0a40e1858d23fef0ffe31fb0 (HEAD -> main) Added file.txt
|
|
|
+
|
|
|
+Though, if we check the status:
|
|
|
+
|
|
|
+$ git status
|
|
|
+On branch main
|
|
|
+nothing to commit, working tree clean
|
|
|
+
|
|
|
+There's nothing to commit, because the change that was made to the file in the
|
|
|
+previous commit was also removed, setting the HEAD back to the first commit:
|
|
|
+
|
|
|
+$ nano file.txt
|
|
|
+Hello World!
|
|
|
+
|
|
|
+Reference: https://stackabuse.com/git-undo-latest-local-commit/
|
|
|
+
|
|
|
+# adding multi-line commit messages
|
|
|
+$ git commit index.js -m "My Changes" -m "- Fixed a critical bug" -m "- Probably
|
|
|
+ added more bugs"
|
|
|
+
|
|
|
+These multiple messages will then look like this in the resulting commit:
|
|
|
+
|
|
|
+My Changes
|
|
|
+- Fixed a critical bug
|
|
|
+- Probably added more bugs
|
|
|
+
|
|
|
+Another option, which depends on the shell you're using, is to just enter a single or
|
|
|
+double quote and press Enter, without closing the quote. This works well in Bash,
|
|
|
+which doesn't enter the command until you've closed the quote:
|
|
|
+
|
|
|
+$ git commit index.js -m "My Changes
|
|
|
+- Fixed a critical bug
|
|
|
+- Probably added more bugs
|
|
|
+"
|
|
|
+
|
|
|
+And finally, you don't actually need to use the -m flag at all. If you omit this flag
|
|
|
+then Git will automatically open a text editor for you to enter the commit message.
|