Table of Contents
- Delete operation
- Rename operation
- Move operation
- Search operation
- Undo operations
- Some problems with file and folder with git
- Wrapping up
Delete operation
-
Stop tracking a file from the staging area
# with a file git rm --cached <file-path> # with a folder # -r option means recursive git rm -r --cached <folder-path>
-
Remove a file from the local repository
-
First, we need to revert to the previous commit.
git reset --soft HEAD^1
So, our files that are in the staging area.
-
Second, we will remove them from the staging area.
git rm --cached <file-path>
Now, our files appear in the untracking files section. Then, we will remove directly that file.
-
Finally, commit the remaining files and push them to the remote repository.
git commit -m "message" git push origin <branch-name>
-
-
Remove files that are not tracked by the staging area
git clean -f # delete folder git clean -f -d # watch files before deleting git clean -n -f -d
-
Delete local’s all removed files in repository
git rm $(git ls-files --deleted)
-
Remove a folder in the remote repository
git rm -rf <folder-name> git commit -m "message" git push -f origin <branch-name>
Rename operation
# rename a file
git mv <file-name> <new-file-name>
Move operation
git mv <file-path> <folder-path>
Search operation
-
Search files’s name that is managed by Git
git ls-files | grep <file-name>
-
Search files that contain words
git grep <word-pattern> # accompany with the number of line git grep -n <word-pattern> # only list the files's name git grep -l <word-pattern> # list the amount of word's appearance git grep -c <word-pattern>
-
Search the user that remove a file
# When a file was removed git log --diff-filter=D -- <file-path> # find the datetime and what's commit that file was deleted git log --diff-filter=D --summary -- <file-path>
The meaning of above options:
- –: It is used to notify Git that it’s not a branch or is an option of a command.
- –summary: list files that were deleted or added. We have another option with the similar functionality: –name-status.
- –diff-filter: specify the type of change a file. It has some values such as Added - A, Coppied - C, Modified - M, Renamed - R.
-
Search information of a file that was added
git log --diff-filter=A --name-status | grep -C 10 <pattern>
-
Find information of user that has the last change for a line of a file
git blame <file-path>
-
Search commits that removed or added a sequence of characters
Using **git log -S
** or **git log -G **. git log -Sgoogle --diff-filter=M --patch
-
Search for commits by a particular author
git log --author="<pattern>"
-
Search for commits with a commit message that matches <pattern>
git log --grep="<pattern>"
-
Display commits that have the specified file
git log -- <file-path>
Undo operation
-
Restore a file that does not add to the staging area
# 1st way - use git checkout git checkout -- <file-path> # 2nd way - use git reset git reset
-
Restore a file that is added to the staging area.
# 1st way - using git restore git restore --staged <file-path> # 2nd way - using git reset in mixed mode # the local respority and the staging area will be reset, but the changes in working space are still remained git reset -- <file-path>
-
Restore a file that is pushed to the local/remote repository
Sometimes we push the changes of a files as the 2 commits to the local/remote repository. But now we want to revert this file to the previous state of the 2 commits.
# commits of a file in the master of the local/remote repository HEAD (refers to branch 'master') | v a --- b --- c branch 'master' (refers to commit 'c')
# switch to the master branch git checkout master # reverts to two revision back git checkout master~2 # delete file in the workspace rm -f <file-name> # restore <file-name> from the staging area git checkout <file-name> # push to the remote repository git push -f origin master
OR we can use the below way:
git checkout master # In git, -- before the file tells git that all the next arguments should be interpreted as filenames, # not as branch-names or anything else. git checkout <commit-id> -- <file-name>
Some problems with file and folder with git
-
File name too long.
Git has a limit of 4096 characters for a filename, except on Windows when Git is compiled with msys. It uses an older version of the Windows API and there’s a limit of 260 characters for a filename.
git config --system core.longpaths true
If it doesn’t work, we can follow the command below:
git config --global core.longpaths true
Wrapping up
- To use above commands effectively, we need to think about what object that we cope with, and then action for it.
Refer:
https://dodangquan.blogspot.com/2017/11/tim-kiem-voi-git.html
https://stackoverflow.com/questions/22575662/filename-too-long-in-git-for-windows