Git - kaushikdas/TechnicalWritings GitHub Wiki
- System (Global)
- Location
- Linux:
/etc/gitconfig
- Windows:
Program Files\Git\etc\gitconfig
- Linux:
- Command to edit:
git config --global
(global
is a modifier)
- Location
- User
- Location
- Linux:
~/.gitconfig
- Windows:
<User Settings Folder>\.gitconfig
- Linux:
- Command to edit:
git config --user
- Location
- Project
- Location:
<git_project>/.gitconfig
- Command to edit:
git config
(this the default one and therefore does not need any modifier)
- Location:
- Examples:
$ git config --global user.name "Kaushik Das" # set user name $ git config user.name # will show set config item (user.name) Kaushik Das $ git config --global user.email "[email protected]" # set user email $ git config --global code.editor "notepad.exe" # sets the editor $ git config --global color.ui true # sets multi-color for UI $ git config --list # will show list of all config items
- By pressing
TAB
key for Git command - For Linux and Mac, for Windows it will be enabled by default
# Edit ~/.bashrc or ~/.bash_profile
if [ -f ~/.git-completion.bash ]; then # downloaded this file from git's github repo
source ~/.git-completion.bash
fi
$ git log -n 5 # View recent FIVE commits
$ git log --since=2019-01-01 # Commits from 01 Jan, 2019
$ git log --until=2019-01-01 # Commits till 01 Jan, 2019
$ git log --since=2019-01-01 --until=2019-01-05 # Commits between 01-05 Jan, 2019
$ git log --author="Kaushik" # commits from "Kaushik" (any part of the name)
$ git log --grep="bug fix" # commit for "bug fix" (in commit message)
add commit
--------> -------->
WORKING STAGING INDEX REPOSITORY
\ ^ /
\ | /
~----<-<-------*------<-<--------*
NOTE
-
git commit -a
orgit commit --all
stages and commits ALL changes to tracked files (i.e., directly takes changes from WORKING to REPOSITORY)- Untracked files (like a newly created file) are not included
- Use
git commit -am
to include the commit message right with the command
$ git diff # difference between WORKING and STAGING
$ git diff --staged # difference between STAGING and REPOSITORY
$ git diff --color-words # difference pin pointed within a line
- The diff is displayed in paginated fashion
- Press
b
(orf
) to navigate back (or front) between the different changes - Press
-S
to toggle line wrapping while viewing diff
- Press
$ git show 1c16564 # Shows the content of the commit with SHA 1c16564 (either full SHA or first 6-8 chars)
$ git show 1c16564 --color-words # difference pin pointed within a line
$ git diff 1c16564..9cdff6 --color-words # Changes between two commits: 1c16564 (from commit SHA) - 9cdff6 (to commit SHA)
$ git diff 1c16564..HEAD --color-words # Changes between two commits: 1c16564 (from commit SHA) - HEAD (latest commit)
- In WORKING:
git checkout -- <filename>
-
--
indicates current working branch, git will checkout the file from the current working branch of the REPOSITORY to WORKING
-
- In STAGING:
git reset HEAD <filename>
(this will unstage the change to<filename>
)
- Amend only latest commit
- [Do the changes] ->
git commit --amend -m "New commit msg"
- [Do the changes] ->
Scenario:
- Revert
file2.java
to the version that is in commit_id_1 [and then do some changes on that]- This will delete the changes in
file2.java
as part ofcommit_id_2
andcommit_id_3
- This will delete the changes in
commit_id_1 ----> commit_id_2 ----> commit_id_3 (latest commit)
FILE(S) CHANGED file1.java file3.java file1.java
file2.java file2.java file2.java
file3.java
Steps
-
git checkout <SHA_of_commit_id_1> -- file2.java
(--
indicates the current working branch)This will revert
file2.java
in WORKING directory (and in STAGING) to the version incommt_id_1
-
[Do some changes to
file2.java
] -
git add file2.java
-
git commit -m "commit msg"
At this stage if we change our mind and want to clean everything we can do:
git reset HEAD file2.java
-
git checkout -- file2.java
To revert a complete commit usegit revert <SHA_of_commit_to_revert>
-
git clean -f
-
-f
will delete all untracked files permanently -
-n
will do a dry run -
-i
is interactive version
-
git config -–global core.excludesfile <git ignore file path>
.
For example:
git config -–global core.excludesfile ~/.gitgnore
Create an empty file (conventionally the file name .gitkeep
is used) inside empty directory to be tracked.