I have a homework assignment where I need to make three different implantations to the same program. The professor recommended using git and having each implantation in a different branch. Thing is that when I do a change on a branch named A, it'll also modify the same file in the master branch...I don't want it to carry my changes around every branch, instead keep the changes saved locally on each branch alone. How do i do this ?
(I'm new to using git) (We work on linux, a remote server, on the terminal)
EDIT: The commands i used to make my project directory:
git init
git commit -m "my message"
git checkout // to switch branches
git branch branchname // to create a branch
When I do:
$ git init
Initialized empty Git repository in MyPath/Test1/.git/
then I create a file test.txt
$ touch test.txt
$ git add test.txt
$ git commit -m " commit 1 "
Now I want to modify it in a different branch
$ git checkout -b branch1
$ echo "Branch 1" >> test.txt
Here it's the tricky part...
If I don't add the file using git add test.txt
and I don't commit but directly go bak to master:
$ git checkout master
Switched to branch 'master'
M test.txt
I will see the modifications in master
!!:
$ git status
On branch master
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: test.txt
What I should do is to add
and commit
in branch1:
$ git checkout branch1
$ git add test.txt
$ git commit -m "commit from branch 1"
[branch1 1b562f5] commit from branch 1
1 file changed, 1 insertion(+)
$ git log
commit 1b562f5be40a50df04f8a2a15b58a7af61742275 (HEAD -> branch1)
Author: xxxxxx<xxxxx.xxxx.xxx@xxxx.com>
Date: Thu Jun 3 16:36:30 2021 +0200
commit from branch 1
commit 03684e2a02c1a37a8f4546f296c6802686c7a4e9 (master)
Author: xxxx<xxx.xx.xx@xxxx.com>
Date: Thu Jun 3 16:31:05 2021 +0200
commit 1
And if I go back to master
and check the log:
$ git checkout master
Switched to branch 'master'
$ git log
commit 03684e2a02c1a37a8f4546f296c6802686c7a4e9 (HEAD -> master)
Author: xxxxx<xxxxx.xxxx.xxxxx@xxxx.com>
Date: Thu Jun 3 16:31:05 2021 +0200
commit 1
We won't see the modification ... as expected
SO! what if you want to work on other branches even if you haven't finished with the current branch?
I go back to branch1 and cat
its content
$ git checkout branch1
Switched to branch 'branch1'
$ cat test.txt
Branch 1
keep editing
$ echo "Branch 1..1" >> test.txt
Lets add
but not commit
and try to checkout
master
$ git add test.txt
$ git checkout master
error: Your local changes to the following files would be overwritten by checkout:
test.txt
Please commit your changes or stash them before you switch branches.
Aborting
not posible! you have to commit
before, BUT! you don't want a new commit every tima you change branche, so you can just commit --amend
$ git commit --amend
$ git log
commit 40a3a66e2b45a253f9a6ed564e977887c0748bf0 (HEAD -> branch1)
Author: xxxxx<xxxxx.xxxxx.xxxxx@xxxxx.com>
Date: Thu Jun 3 16:36:30 2021 +0200
commit from branch 1 => I can edit the comment!!
commit 03684e2a02c1a37a8f4546f296c6802686c7a4e9 (master)
Author: xxxxx<xxxxx.xxxxx.xxxxx@xxxxx.com>
Date: Thu Jun 3 16:31:05 2021 +0200
commit 1
And now i can safely git checkout master