Git - kaushikdas/TechnicalWritings GitHub Wiki

1. Git Configuration

  • System (Global)
    • Location
      • Linux: /etc/gitconfig
      • Windows: Program Files\Git\etc\gitconfig
    • Command to edit: git config --global (global is a modifier)
  • User
    • Location
      • Linux: ~/.gitconfig
      • Windows: <User Settings Folder>\.gitconfig
    • Command to edit: git config --user
  • Project
    • Location: <git_project>/.gitconfig
    • Command to edit: git config (this the default one and therefore does not need any modifier)
  • 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

2. Git Auto-completion

  • 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 

3. View Git log

$ 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)

4. Git "Three Trees"

                add                     commit  
             -------->                 -------->
    WORKING             STAGING INDEX             REPOSITORY
             \                ^                   /
              \               |                  /
               ~----<-<-------*------<-<--------*

NOTE

  • git commit -a or git 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

5. Seeing the changes

$ 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 (or f) to navigate back (or front) between the different changes
    • Press -S to toggle line wrapping while viewing diff

5.1. See committed changes

$ 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)

6. Reverting changes

  • 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>)

7. Amend commit

  • Amend only latest commit
    • [Do the changes] -> git commit --amend -m "New commit msg"

8. Retrieve old version

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 of commit_id_2 and commit_id_3
                     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

  1. 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 in commt_id_1

  2. [Do some changes to file2.java]

  3. git add file2.java

  4. git commit -m "commit msg"

    At this stage if we change our mind and want to clean everything we can do:

    1. git reset HEAD file2.java
    2. git checkout -- file2.java To revert a complete commit use git revert <SHA_of_commit_to_revert>

Remove untracked files

  • git clean -f
    • -f will delete all untracked files permanently
    • -n will do a dry run
    • -i is interactive version

Global gitignore

git config -–global core.excludesfile <git ignore file path>.

For example:

git config -–global core.excludesfile ~/.gitgnore

Tracking empty directory

Create an empty file (conventionally the file name .gitkeep is used) inside empty directory to be tracked.

⚠️ **GitHub.com Fallback** ⚠️