How to add untracked file in last git commit? - version-control

I initiated git, then used following command to add all the files in commit.
git add *
git commit -m "Starting project modifications"
but still there is .htaccess file as untracked, I don't know why it was not added now I want to add it to my last commit. How can I add it without doing another commit.

As mentioned here:
https://www.atlassian.com/git/tutorials/rewriting-history/git-commit--amend
Following is the example:
# Edit hello.py and main.py
git add hello.py
git commit
# Realize you forgot to add the changes from main.py
git add main.py
git commit --amend --no-edit

Related

Pushing folder into github

I was trying to push the folder on my computer to GitHub. So I created a GitHub repository, and use git bash command line. I didn't push the folder successfully on my first try. Then, I deleted the old GitHub repository and created a new one, and tried using the git bash command line to push code again. However, it shows nothing to commit.
Here is an image to better help understand
According to the image, I understand that you have made a commit but your commit was empty and you did not track any file with git beforehand. You typically want to track the files you want to commit. So in this case you could use git add before committing:
git add .
This should track all files in the current folder after which you could commit and push them:
git commit -m "Some message"
git push
When you create a new repository on git, it shows you how to properly upload data
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/youracc/your.git
git push -u origin main

How can I git push to a specific folder?

I'm using Travis CI to build my Jekyll site and push to Github. A lot of documentation around is for keeping the Jekyll code in the master branch, and building the site and pushing _site to the gh-pages branch. I want to avoid doing that and use the username.github.io repo on Github.
This is currently my build script:
#!/bin/bash
# skip if build is triggered by pull request
if [ $TRAVIS_PULL_REQUEST == "true" ]; then
echo "this is PR, exiting"
exit 0
fi
# enable error reporting to the console
set -e
# cleanup "_site"
rm -rf _site
mkdir _site
# build with Jekyll into "_site"
bundle exec jekyll build
# push
cd _site
git config user.email "john.doe#gmail.com"
git config user.name "John Doe"
git add --all
git commit -a -m "Travis #$TRAVIS_BUILD_NUMBER"
git push origin master
Of course, git push origin master as of now replaces the entire branch itself with the generated site. How do I generate the site and keep it within _site?
I am assuming you are running this script inside some folder. lets call that my_project.
Inside my_folder, when you run this script, it creates a _site folder.
Next you are doing cd _site. Dont do this.
Instead, remain at my_project folder and do the git config steps and others at this level.
So this will push all the stuff inside this my_project folder and the site will be inside the _site folder.
TL,DR:
Change these lines:
git add --all
git commit -a -m "Travis #$TRAVIS_BUILD_NUMBER"
To:
git add ./ #I assume you've cd'd into the directory you want to commit.
git commit -m "Travis #$TRAVIS_BUILD_NUMBER"
Explanation:
This question smells like a problem that someone coming from svn would have.(Skip the next paragraph if you aren't coming from svn)
There is a big difference in the way svn and git handle commits.
When you use svn, the commit always works relative to the current directory. So, the line cd _site changes what is known as the present working directory to _site. And there on out, svn only is concerned about the current directory and its children.
On the other hand, git commands will climb the directory path till they reach the root. So, when you are in _site and give a command with a --allor -a it will commit all the changes in the whole branch.
So, you replace --all with ./ in the add command which is just linux shorthand for the present directory. And remove the -a in the commit command. So the "changes added" are only from the present directory ie _site. This stages the changes for commit. You remove the -a from the commit command so that it only commits the staged changes.
As an aside, git add --all followed by git commit -a is redundant. The -a in commit ignores what is added to the stage and just commits all changes.

Github file removal after push

I accidentally pushed a file to my Github repository, and I am trying to understand where it is.
I have read many posts in here, but none of them had the solution, so I wanted to ask.
My steps were:
git status
git add normal_file.txt
git add sensitive_content_file.txt
git rm --cached sensitive_content_file.txt
#It was the time that I realized I have added a wrong file and need to remove it.
git commit -m "new changes"
git push
I saw that it pushed the "normal_file.txt " into my repository however I have not seen the "sensitive_content_file.txt" in my repository, so I assume it is not being pushed? So that all is okay?
I have tried to clean cache but now my repository complains as:
" Your branch and 'origin/master' have diverged, and have 1 and 1 different commit each, respectively."
However, I did not do any changes to itself.
How can I clear every connection to this file with I tried to remove its caches?
git rm --cached sensitive_content_file.txt before the commit was enough to not pushing it to GitHub.
But now, it would be best to add that file to your .gitignore, in order to be sure to not add it again by mistake:
echo sensitive_content_file.txt>>.gitignore
git add .gitignore
git commit -m "Ingore sensitive_content_file.txt"
git push
If you did any rebase/commit --amend, that would change the local history compared to what was pushed.
If you have not done any local modification, a git reset --hard origin/master would reset master, then you can modify the .gitignore and push.

How to upload all folder to github?

I have created a repository on github, and run the command line git push -u origin master. I just find a folder over there, but I have a lot of file in my folder, but it only uploaded a folder that has no files in it.
How do I upload a folder with lots of files successfully?
I just tried the command git add . and git push -u origin master
It's not working.
Please me teach me what step I missed. Thanks in advance.
You need to add all of the files in the folder. If you actually want to add all of it (not just some), an easy way to do that would be git add -a or git add ReduxSimpleStarter/*.
Then you need to commit them. git commit.
Then push.
I'm guessing, by your description (sorry, it's a bit hard to read. So correct me if I am missing some details to your problem) you're forgetting to commit the changes.
To do this you need to git commit -m "A commit message". It would look something like this all together
git add .
git commit -m "Added a new button"
git push -u origin master
commit just tells git to save the changes you made to it's history. add just tells git that you want those files (in their current state) to be saved next time you commit.
Hopefully that solves your problem.
It looks like you are not committing your changes before pushing.
When you do this:
git add .
your files are adding to the staging area. But you also need to commit them. You can do this by typing this into you command line after doing the 'git add .' command:
git commit -m "your commit message here"
Then you should be able to type 'git push -u origin master' and your files should upload.

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