Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

gitlab - How merging works when we use git add <filename>?

Assume this is my project structure:

- project
    - .git
    - scr
        - component
            - file.js
  • I did clone the project
  • Made a new branch by git checkout -b "newBranch"
  • Made some changes on file.js
  • Added it to the stage by git add /scr/compoent/file.js
  • Then committed it by git commit -m "some changes"
  • Finally pushed it on the server by git push origin newBranch

From the beginning of my life until now, I only was using git add . for adding changes to the stage. Few mins ago, for the first time, I used git add file.js instead.

After those commands, I went to the GitLab interface -> Repasitory -> File -> newBranch and I saw ONLY file.js there. Also, made a merge request by clicking on create merge request, then assigned it to me (myself) and put master as the destination branch. After merging, the project structure turned out like this:

- project
    - .git
    - file.js              //=> The file contains the changes
    - scr
        - component
            - file.js      //=> This file remained untouched

My question is, why file.js is pasted in the root of the project? How can we apply changes on the real file in such cases instead of creating an instance file (containing changes) and locate it on the root?

question from:https://stackoverflow.com/questions/65921715/how-merging-works-when-we-use-git-add-filename

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is not what should happen. IMHO you have saved file.js to the root after doing your changes using "Save as..." instead of saving .

There is also something wrong in your description. git add /scr/compoent/file.js seems to be wrong as there should not be leading slash and there is a typo as well.

Using git add scr/component/file.js shoudl work correctly.

Using git add file.js in the top level directory would stage the file in the top level directory that you probably created by mistake...

Always study output of git status before adding and commiting...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...