correct usage of .gitignore (for file types) - gitignore

when I type git status I get the following message:
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: .DS_Store
modified: README.txt
modified: lib/PriceSeries.rb
Untracked files:
(use "git add <file>..." to include in what will be committed)
MM8 Prices.pdf
prices from EH.xlsx
I want to be able to say git add ., without adding the untracked files. I believe that the .gitignore file is supposed to be for this. My .gitignore file (in the .git folder) looks like this:
#Ignore all xlsx files
.xlsx
#Ignore all pdf file
.pdf
What am I doing wrong?

Prefix your extensions with *:
#Ignore all xlsx files
*.xlsx
#Ignore all pdf file
*.pdf
[Update] Also, .gitignore must be in the root of your project, not under .git

Related

change branch master in vs code

New to using github in VS Code and accidentally did git init on a high level folder when in fact i meant a low level sub folder. I've tried to do everything i can to un-assign this folder as it has over 5000 items in (.js libraries etc!) but can't seem to google the right solution. If i check git status in the terminal window i get
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
FreeCodeCamp/
folder1/
Test web/
Web cheat sheet.html
index.html
my_profile_pic-min.jpg
my_profile_pic.jpg
folder2/
bio/
blog/
nothing added to commit but untracked files present (use "git add" to track)
PS D:\Web Design>
Not really sure how to remove/change the master folder to the freecodecamp sub folder.
All help welcome.
Thanks.
You can simply delete the .git directory in the high level directory to stop git tracking there, and then do git init again in the appropriate sub-directory to initialize that as a git repository.

gitignore unable to ignore files under certain directories

How do I ignore all folders/files under audit directory?
I tried adding **/audit to .gitignore which is present in the root of my project. where .git is present but the file is still getting displayed in unstaged changes in eclipse. Is it because of the - in file name?
data/test/audit/portfolio-tnc/TEST-2019-04-22T03-20-31.json
If the file is already tracked by git you have to remove it from git index
git rm --cached data/test/audit/portfolio-tnc/TEST-2019-04-22T03-20-31.json
This will not remove the file but git won’t track it anymore.
You can untrack a whole directory with
git rm -r --cached data/test/audit

git is not ignoring an eclipse .version file

It may be something simple, but I cannot make git ignore this. I have put different combinations of things into the .gitignore file in the directory at the root of my repository.
But I still see:
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
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: .gitignore
modified: MyProject/build/.version
no changes added to commit (use "git add" and/or "git commit -a")
$
I have "build" in my .gitignore file. I have tried "MyProject/build/.version", "build/.version", ".version" and various combinations thereof. But perhaps not the right combination.
If you've only just added build to your .gitignore, then the files which were tracked before will still be tracked. .gitignore only prevents git from tracking new, untracked files which match the pattern given. To remove the file from git's history, without removing the file locally, you can run
git rm -r --cached build
to remove everything in the build directory, or
git rm --cached MyProject/build/.version
(or whatever the path is) to just remove the one file.
Try entering the following in your .gitignore:
/build
Another thing to try is
build/

GitHub Failed to sync this branch

IM getting this error message that says syncing has failed. I looked around for solutions and found that git status would give some idea about the problem. So I did it and got this:
# On branch dev_0.9_HUD_development
# Your branch is behind 'origin/dev_0.9_HUD_development' by 1 commit, and can be
fast-forwarded.
# (use "git pull" to update your local 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: res/models/hud/HUD2.png
# modified: src/Weapon/Laser.java
# modified: src/Weapon/Weapon.java
# modified: src/game/world/gui/hud/HeadsUpDisplay.java
# modified: src/game/world/gui/hud/ShipStat.java
# modified: src/game/world/gui/hud/WeaponDisplay.java
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# src/hero101.mtl
# src/hero101.obj
There were many solutions but none of them seem to work. I tried restarting computer. Nothing happened. I tried git checkout but it gives another error. I tried merging the branches still get the same error. What can I do to fix this?
If your branch is behind an upstream remote branch, that means you haven't done any commit.
In command line:
cd /patH/to/your/repo
git stash
git pull
git stash pop
git add -A
git commit -m "My work"
git push
That assumes that all the files listed by your current git status are modified files that you want to version.
Ok. So i looked into the .git folder in the repo. I changed the name of index.lock file, after that i did the git stash and git pull and everything worked.

How to use Git for updating my directory and other contents?

I have a directory like below:
/var/tmp/main
.. plugin1
.... mysource.c
.. plugin2
.... mysource.c
.. plugin3
.... mysource.c
And I created a repository. After that, I applied the below:
$ cd /var/tmp/main
$ git init
$ echo "hello git plz work" >> README
$ git add README
$ git commit -m 'first commit works'
$ git remote add origin git#...../main.git
$ git push origin master
I also tried
$ git add .
$ git status
# On branch master
# 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)
# (commit or discard the untracked or modified content in submodules)
#
# modified: folder1 (modified content)
# modified: folder2 (modified content)
# modified: folder3 (modified content)
# modified: folder4 (modified content)
# modified: folder5 (modified content)
# modified: folder6 (modified content, untracked content)
# modified: folder7 (modified content)
# modified: library (modified content, untracked content)
#
no changes added to commit (use "git add" and/or "git commit -a")
$ git commit -m 'Suggested by Stack experts'
$ git push
Tried also .gitignore
$ cat .gitignore
*.[oa]
*.pyc
*.gcda
*.gcno
*.la
*.lo
*.loT
*.sw[po]
*.tar.*
*~
.deps
.libs
ABOUT-NLS
INSTALL
Makefile
Makefile.in
aclocal.m4
autom4te.cache
autoregen.sh
compile
config.guess
config.h
config.h.in
config.log
config.rpath
config.status
config.sub
configure
depcomp
install-sh
libtool
ltmain.sh
missing
stamp-h1
Same so far...
All set but after 14 hours I still do not see all of my directories in Git, I only see the README file showing.
How can I commit all? (following http://scottr.org/presentations/git-in-5-minutes/ )
Here is the simple method to add empty directories:
//create some empty, gitignore files.
touch ./someDir/.gitignore
touch ./someOtherDir/.gitignore
...etc..
git add ./someDir/.gitignore
git add ./someOtherDir/.gitignore
...etc...
git commit -am " Add empty project directories."
This is because, as mentioned in other answers git tracks files in directories, but ignores directories on their own. This also might be fixed/solved in the git add mechanism in more up to date versions (I vaguely remember them adding their own .gitignore files to empty directories), so I'd really recommend trying to upgrade to the highest git version you can for that and other benefites, your distro may provide by default an older version, as ubuntu and debian do.
Edit:
The above technique is designed for the minimal amount of disturbance of your working directories in order to get empty folders tracked. -Each- directory would in my examples get their own .gitignore file (you can have as many .gitignore files as you like, they are additive).
So at the end your folder structure would look like this:
/var/tmp/main
.. plugin1
.... mysource.c
.... .gitignore
.. plugin2
.... mysource.c
.... .gitignore
.. plugin3
.... mysource.c
.... .gitignore
And then you would add the .gitignore files as placeholders!
But let's take a step back and try the opposite tactic:
Maximum visibility.
Go to any folder you want to add, e.g. plugin1 . Create a file in that folder, call it placeholder.
Now navigate to that folder from the command line, e.g. cd /var/tmp/main/plugin1/ and git add that placeholder file, e.g. git add placeholder . You've told git that you want that file tracked (if you type git diff you can review the "proposed" changes. It'll tell you that it sees the file, but that it's just an empty file, which is fine).
Now commit the file: git commit placeholder -m " Adding a Placeholder file."
When you add any file, all the folders that contain that file, down to the main git folder, get added as well, so you'll now have /plugin1/ tracked in git.
Go through, use git add /path/to/file on any of those (?) c source files you have, and then commit the changes, via git commit /path/to/file. Generally, anything that's pure text, it's good to be added to the repository, of course.
Finally: Be aware that git status is only designed to tell you about modified, tracked files, including newly added files. If there are no modifications, it'll just give you mostly blank output.
To see the files that -are- actually tracked by git, use git ls-tree HEAD which will only show you tracked files!
For a clean start
Here is how I generally start a git repository.
cd /path/to/project/
git init
echo "README" >> README
git add .
git commit -am " Initial Commit of readme and files."
You only added your README file. If you type git status, you will see that all of your other files are considered "untracked". You need to add them with git add so they will be tracked:
git add file1 file2 file3
git commit -m 'Added remaining files to repository'
git push
The important point about "git add ." is that it looks at the working tree and adds all those paths to the staged changes if they are either changed or are new and not ignored
And neither Git or Mercurial track empty dirs