0

I check out a branch locally and worked with 2 files, say, their names are A.java and B.java. Then, I add and commit to the commands i.e. git add ., git commit -m "some msg". Eventually, I push them into the remote branch with command git push -u origin {branch_name}.

It creates an embarrassing situation where few maven and IDEA files pushed in the remote. Even, now when I execute the command git status, I get the following output:

chak@debian8:~/Projects/draglet-backend$ git status
On branch FNB-90
Your branch is up-to-date with 'origin/FNB-90'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .idea/
        draglet-backend.iml
        draglet-balser/draglet-balser.iml
        draglet-common/draglet-common.iml
        draglet-engine/draglet-engine.iml
        draglet-mapu/draglet-mapu.iml
        draglet-meba/draglet-meba.iml
        draglet-remote/draglet-remote.iml
        draglet-shaba/draglet-shaba.iml

nothing added to commit but untracked files present (use "git add" to track)

I have few questions in the scenarios to work in the future:

  1. What are the Untracked files here and how are they created? I only write some Java in the project.

  2. How do I set 'git' properly to ignore the maven and idea files like them in the future? I will most likely make changes in the .java and the .yml files.

  3. How do I only add and commit the files that I have worked until the previous commit and also is it possible to partially commit only the lines of code I have written? I know its possible to add the files separately with the command git add [full_path_to_A.java] [full_path_to_B.java]. However, the full paths are super big and there might be a better way to deal with this.

Arefe
  • 11,321
  • 18
  • 114
  • 168

3 Answers3

2

What are the Untracked files here and how are they created? I only write some Java in the project.

Untracked files file exists locally, but isn’t a part of the Git repository. The file’s change history will not be recorded and it will not be pushed to remote copies of your repository when you run git push. Unless they’ve been added to an ignore list, untracked files will show up when viewing your Git status.

How do I set 'git' properly to ignore the maven and idea files like them in the future? I will most likely make changes in the .java and the .yml files.

In the .gitignore file in your local add this line

.idea/

to ignore .idea and also add other regexes to ignore particular files or directories, if you don't want to push them

How do I only add and commit the files that I have worked until the previous commit and also, is it possible to partially commit only the lines of code I have written?

Run the command

git add ./            // stage all files 
git commit -m "Some message"    // commit the files

Check this question on how to commit parts of your file

Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
1

To answer all your questions:

  1. Untracked files are files that are on your file system but they are not in the list of files managed with git. If you want to track them with git you need to git-add them, otherwise to git-ignore them. In your case those are files created by your IDE and should definately be ignored
  2. You should take a look here and here for the information on the .gitignore file
  3. Usually that's what a git commit does. It does only wrap your modifications and then you can push them to the remote repository. However this untracked files you have are considered by git as a "contribution" and so you need to set up your repository in order to ignore them and make them not considered. Once your setup is refined properly you'll just see your desired files and you can commit 'em all. However take a look here if you need more info on the possible flags of the commit commans
rakwaht
  • 3,666
  • 3
  • 28
  • 45
1

As I'm seeing that your question is answered, I would want to add some useful info with a link of useful .gitignore files templates.

https://github.com/github/gitignore

Take it a look, it could be useful for you.

Anup Yadav
  • 2,825
  • 3
  • 21
  • 30
  • This is super helpful and you should have arrived earlier `:-} `. So, if I set the `gitignore` properly, I can just write `git add .` and only the files I have worked on since the last commit will be staged for the commit. If I `commit and push`, only those files will be pushed. Is this correct? – Arefe Feb 16 '18 at 09:26
  • Yep, that's correct, you just need to add the .gitignore file in your project directory where you have initialized your local git, it will automatically reject all files that should be ignored. Have fun coding! –  Feb 16 '18 at 12:37