Git learning notes.
fatal: no files added
When I add files under a directory using
git add some_folder/*, I see the
following error message:
The following paths are ignored by one of your .gitignore files: ppt/__pycache__ Use -f if you really want to add them. fatal: no files added
This is because
* is interpreted by the system, so it will add all files
under a directory. If some files under this directory are in the
you will see the above error message.
git ask for username and password each time I push
I cloned a remote repo using https protocol. Each time when I push to this repo, git asks me to enter my username and password. This is annoying.
To prevent from entering username each time, we can add username to remote repo url like this:
git remote set-url origin https://firstname.lastname@example.org/jdhao/toy_project.git
jdhao is my username. In this way, we do not need to enter username
when we push to remote. In fact, we can provide the password after the username
git remote set-url origin https://jdhao:email@example.com/jdhao/toy_project.git
However, it is not safe to do so, since your password are stored in plain text now.
For password, if we are in Linux, we can use the following settings:
git config --global credential.helper 'cache --timeout=3600'
The password will be cached in memory for 3600 seconds, which means that you do not need to input password for 1 hour. Tweak the time interval to suit your need.
- Is there a way to cache GitHub credentials for pushing commits?
Compare same file from different branches
Sometimes, I want to see the difference of a file on different branches. For
example, to find the difference for
foo.py on master and dev branch. There
are two slightly different ways to do this.
git diff master..dev path/to/foo.py git diff master dev -- path/to/foo.py
git diff master:path/to/foo.py dev:path/to/foo.py
Clone a git repository without history?
Due to slow networking speed or other reasons, we may want to clone a
repository without its huge history. We can use
--depth to restrict the
number of history to include,
--depth 1 will only clone the last commit,
effectively meaning that no history is cloned.
git clone --depth 1 http://url/to/git/project
- Clone git repository without history?
How to check last commit for a given line only?
By default, git-blame will print
out the last commit info for each line in a file. What if we are only
interested in a particular line range? In this case, we can use
restrict the line range. For example, if I want to blame line 4-5 for file
options.vim, I can use the following command:
git blame -L 4,5 core/options.vim
License CC BY-NC-ND 4.0