1. Branching.
Literally means branching.
You don't always want to work on your master branch. Sometimes, you want to deviate from the project and try out something new. Then, you branch. So type in: git branch your_new_branch_name. This is basically going to copy the whole content of the master branch into the new branch.
2. Try and recognize the change. FYI, git checkout new_branch will bring you to that new_branch so that you could work in there. :
***** in ~/Desktop/dummy on master*
$ git branch
* master
***** in ~/Desktop/dummy on master*
$ git branch new_branch
***** in ~/Desktop/dummy on master*
$ git checkout new_branch
M test.txt
Switched to branch 'new_branch'
***** in ~/Desktop/dummy on new_branch*
$ git branch
master
* new_branch
3. git merge branch_name will merge any changes made to that branch to the master branch. But be reminded that, if you want to merge the branch to a master branch, you should do it when you are on the master branch (so do git checkout master first before anything):
***** in ~/Desktop/dummy on new_branch*
$ nano thisWasMadeForMerging.txt
***** in ~/Desktop/dummy on new_branch*
$ ls
anotherFileToAdd.txt test.txt thisWasMadeForMerging.txt
***** in ~/Desktop/dummy on new_branch*
$ git status
On branch new_branch
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: test.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
anotherFileToAdd.txt
thisWasMadeForMerging.txt
no changes added to commit (use "git add" and/or "git commit -a")
***** in ~/Desktop/dummy on new_branch*
$ git add anotherFileToAdd.txt thisWasMadeForMerging.txt
***** in ~/Desktop/dummy on new_branch*
$ git commit -m "change for merging"
[new_branch 36851a7] change for merging
Committer: 9oelM <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:
git config --global user.name "Your Name"
git config --global user.email [email protected]
After doing this, you may fix the identity used for this commit with:
git commit --amend --reset-author
2 files changed, 2 insertions(+)
create mode 100644 anotherFileToAdd.txt
create mode 100644 thisWasMadeForMerging.txt
***** in ~/Desktop/dummy on new_branch*
$ git checkout master
M test.txt
Switched to branch 'master'
****** in ~/Desktop/dummy on master*
$ git merge new_branch
Updating 7eed22f..36851a7
Fast-forward
anotherFileToAdd.txt | 1 +
thisWasMadeForMerging.txt | 1 +
2 files changed, 2 insertions(+)
create mode 100644 anotherFileToAdd.txt
create mode 100644 thisWasMadeForMerging.txt
$ git log
commit 36851a72a6d5450e06365d25208155d6663ec77f
Author: **** <******.local>
Date: Fri Apr 7 19:02:52 2017 +0800
change for merging
commit 7eed22f0fb2862581a83dbefb25bcb44d28450ba
Author: **** <******.local>
Date: Fri Apr 7 17:57:23 2017 +0800
test complete
4. merge conflict. what if you have changed master branch already, but you have also changed the new_branch as well and they are not the same? then this is a merge conflict.
Literally means branching.
You don't always want to work on your master branch. Sometimes, you want to deviate from the project and try out something new. Then, you branch. So type in: git branch your_new_branch_name. This is basically going to copy the whole content of the master branch into the new branch.
2. Try and recognize the change. FYI, git checkout new_branch will bring you to that new_branch so that you could work in there. :
***** in ~/Desktop/dummy on master*
$ git branch
* master
***** in ~/Desktop/dummy on master*
$ git branch new_branch
***** in ~/Desktop/dummy on master*
$ git checkout new_branch
M test.txt
Switched to branch 'new_branch'
***** in ~/Desktop/dummy on new_branch*
$ git branch
master
* new_branch
3. git merge branch_name will merge any changes made to that branch to the master branch. But be reminded that, if you want to merge the branch to a master branch, you should do it when you are on the master branch (so do git checkout master first before anything):
***** in ~/Desktop/dummy on new_branch*
$ nano thisWasMadeForMerging.txt
***** in ~/Desktop/dummy on new_branch*
$ ls
anotherFileToAdd.txt test.txt thisWasMadeForMerging.txt
***** in ~/Desktop/dummy on new_branch*
$ git status
On branch new_branch
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: test.txt
Untracked files:
(use "git add <file>..." to include in what will be committed)
anotherFileToAdd.txt
thisWasMadeForMerging.txt
no changes added to commit (use "git add" and/or "git commit -a")
***** in ~/Desktop/dummy on new_branch*
$ git add anotherFileToAdd.txt thisWasMadeForMerging.txt
***** in ~/Desktop/dummy on new_branch*
$ git commit -m "change for merging"
[new_branch 36851a7] change for merging
Committer: 9oelM <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:
git config --global user.name "Your Name"
git config --global user.email [email protected]
After doing this, you may fix the identity used for this commit with:
git commit --amend --reset-author
2 files changed, 2 insertions(+)
create mode 100644 anotherFileToAdd.txt
create mode 100644 thisWasMadeForMerging.txt
***** in ~/Desktop/dummy on new_branch*
$ git checkout master
M test.txt
Switched to branch 'master'
****** in ~/Desktop/dummy on master*
$ git merge new_branch
Updating 7eed22f..36851a7
Fast-forward
anotherFileToAdd.txt | 1 +
thisWasMadeForMerging.txt | 1 +
2 files changed, 2 insertions(+)
create mode 100644 anotherFileToAdd.txt
create mode 100644 thisWasMadeForMerging.txt
$ git log
commit 36851a72a6d5450e06365d25208155d6663ec77f
Author: **** <******.local>
Date: Fri Apr 7 19:02:52 2017 +0800
change for merging
commit 7eed22f0fb2862581a83dbefb25bcb44d28450ba
Author: **** <******.local>
Date: Fri Apr 7 17:57:23 2017 +0800
test complete
4. merge conflict. what if you have changed master branch already, but you have also changed the new_branch as well and they are not the same? then this is a merge conflict.