Why should I work on a non-master branch?
Now I am working on different machines, I just wanna distinguish which part is modified by which machine.
0. clone a repository
I'll start with cloning a remote repository from oschina git clone https_url
enter the username and passwd when requested
1. establish a new branch
use git checkout -b branchnamename
to create branches
mark@ASUS:~/Documents/Paper/trans$ git checkout -b ubuntu
M articlefile.tex
Switched to a new branch 'ubuntu'
by git -checkout -b xxx
we create and automatically switch to the new branch.
use git branch
to see the branch name you are working on
mark@ASUS:~/Documents/Paper/trans$ git branch
master
* ubuntu
mark@ASUS:~/Documents/Paper/trans$
Now I have two branches, one is master
and the other is ubuntu
, and I am now working the ubuntu
branch (a star sign indicate the current brnanch you are working on). Make a little change to the articlefile.tex
.
by typing git status
, the terminal will show a detailed massage about the current branch, together with the status info.
mark@ASUS:~/Documents/Paper/trans$ git status
On branch ubuntu
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: articlefile.tex
Untracked files:
(use "git add <file>..." to include in what will be committed)
articlefile.fls
no changes added to commit (use "git add" and/or "git commit -a")
2. working on a non-master branch
I ssume you have known how to make a commission to the remote repository. But it does not matter if you really do not know, a series of command is needed as follows in order:
git add
, git commit
, and git push
It's not different from working on the master branch to some extent.
at any stage, you can type git status
to see what exactly happened.
git add
mark@ASUS:~/Documents/Paper/trans$ git add *.tex
mark@ASUS:~/Documents/Paper/trans$ git status
On branch ubuntu
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: articlefile.tex
Untracked files:
(use "git add <file>..." to include in what will be committed)
articlefile.fls
git commit
mark@ASUS:~/Documents/Paper/trans$ git commit -m "commit to Ubuntu"
[ubuntu 60d8952] commit to Ubuntu
1 file changed, 1 insertion(+), 1 deletion(-)
git push
mark@ASUS:~/Documents/Paper/trans$ git push
warning: push.default is unset; its implicit value has changed in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the traditional behavior, use:
git config --global push.default matching
To squelch this message and adopt the new behavior now, use:
git config --global push.default simple
When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.
Since Git 2.0, Git defaults to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch.
See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)
fatal: The current branch ubuntu has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin ubuntu
git push --set-upstream
It's obvious that the commission is not successful, 'cause a fatal message is here. It shows that The current branch ubuntu has no upstream branch. and it also tell me to use git push --set-upstream origin ubuntu
to push the current branch.
mark@ASUS:~/Documents/Paper/trans$ git push --set-upstream origin ubuntu
Username for 'https://git.oschina.net': markhan
Password for 'https://markhan@git.oschina.net':
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 353 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To https://git.oschina.net/markhan/fuidlike-PC.git
* [new branch] ubuntu -> ubuntu
Branch ubuntu set up to track remote branch ubuntu from origin.
OK, now it has been pushed to the upstream.
mark@ASUS:~/Documents/Paper/trans$ git status
On branch ubuntu
Your branch is up-to-date with 'origin/ubuntu'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
articlefile.fls
nothing added to commit but untracked files present (use "git add" to track)
The local branch has been upload to the remote server, and it is now uptodate
3. continue to make changes on the non-master branch
Continue to make some changes to the articlefile.tex
file, when I type git status
it shows the file has been modified.
mark@ASUS:~/Documents/Paper/trans$ git status
On branch ubuntu
Your branch is up-to-date with 'origin/ubuntu'.
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: articlefile.tex
Untracked files:
(use "git add <file>..." to include in what will be committed)
articlefile.fls
no changes added to commit (use "git add" and/or "git commit -a")
git diff
to see the differences
mark@ASUS:~/Documents/Paper/trans$ git diff articlefile.tex
diff --git a/articlefile.tex b/articlefile.tex
index c4e3ada..6fb9b04 100644
--- a/articlefile.tex
+++ b/articlefile.tex
@@ -418,5 +418,6 @@ Pentamode materials support only longitudinal waves for the
The theory of PC has been developed over 20 years,
\bibliography{../metabib/META}
+% \bibliography{C:/EndNote/META}^M
\end{document}
mark@ASUS:~/Documents/Paper/trans$ ^C
4. git branch
vs git checkout
| master branch vs non-master branch branches
- to create a new branch we use
git branch
, and to switch between different branches we usegit checkout
- The new branch we have created is, to some extent, independent of the master branch. In fact, all the branches are independent.
mark@ASUS:~/Documents/Paper/trans$ git branch master
fatal: A branch named 'master' already exists.
# `git branch` is for creating branch
mark@ASUS:~/Documents/Paper/trans$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
# `git checkout` is for switching from one branch to another branch
mark@ASUS:~/Documents/Paper/trans$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
articlefile.fls
nothing added to commit but untracked files present (use "git add" to track)
# the .fls is not included in the .gitignore file on the master branch
mark@ASUS:~/Documents/Paper/trans$ git checkout ubuntu
Switched to branch 'ubuntu'
Your branch is up-to-date with 'origin/ubuntu'.
# now turn to the ubuntu branch
mark@ASUS:~/Documents/Paper/trans$ git status
On branch ubuntu
Your branch is up-to-date with 'origin/ubuntu'.
nothing to commit, working directory clean
# 'cause the .fls has been added to the .gitignore file on the ubutnu branch, articlefile.fls is ignored automatically on the ubuntu branch.
网友评论