Skip to content

This repository is a comprehensive collection of my Git knowledge, including commands, best practices, tutorials, and tips. It serves as a personal reference and a resource for anyone looking to enhance their Git skills.

Notifications You must be signed in to change notification settings

saurabhssahu/git

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Git Commands & Notes

Courtesy: The Git & GitHub Bootcamp - Colt Steele


Notes

  • HEAD refers to a branch reference and not a commit and the branch reference points to a commit

    • In detached HEAD, it refers to a commit hash
  • gitignore.io - it provides a general git ignore file for whatever programing language we choose

  • git rm -r --cached .- removes all tracked files from the staging area without deleting them from the local filesystem. Useful when we want to reflect the newly added gitignore or made some changes in it.

  • Rewriting History - to delete the previous commit

    • git reset --hard HEAD~1
    • git push origin HEAD --force
  • Travelling in time

    • git checkout <commit_hash>/ HEAD~1/2/3- moves in detached HEAD state
    • git switch <branch_name> or git switch- will re-attach the HEAD
  • git rebase vs git merge

    • git merge preserves the history
    • git rebase re-writes the history
    • git merge <branch_name>- merge <branch_name> into whatever branch you are in
    • git rebase -i HEAD~4 - interactive rebase a series of commits onto the HEAD(upto 4 commits) in the same branch
  • git clean - to remove untracked files

    • git clean -n- to do a dry run
    • git clean -d- to clean untracked directories
    • git clean -f- to force clean
    • These public webpages thar are hosted and published via GitHub.

    • These are static webpages means just HTML/CSS/JS and does not support server-side code.

    • Helpful for creating portfolio websites or documentation websites for your repositories.

    • <gh-username>.github.io- user sites : allowed one per GitHub account. Ex: saurabhssahu.github.io

    • <gh-username>.github.io/repo-name- project sites : https://saurabhssahu.github.io/git/
      NOTE: Enable GitHub Pages properly through Settings by routing to the correct directory where the HTML, CSS, and JS code reside.


Commands

  • git config --global user.name
  • git config user.name- at the root level of project to override the default settings
  • git config --global user.email
  • git config --local user.name- for local git config (within a repository)
  • git config --global core.editor "code --wait"- to set VS Code as default editor
  • git config --global core.excludesfile [file]- to store global ignore file
  • git config --global alias.s status- setting alias for git status. After configuring git s will be equivalent to git status

Reference to git aliases
Some common aliases

git a - git add
git b - git branch
git c - git commit
git d - git diff


git remote

  • git remote add <remote_name> <url>- standard name is origin, or we can give upstream
  • git remote set-url origin <new-url>- to change the remote url
  • git remote rename <old> <new>- to rename remote
  • git remote remove <name>- to remove remote

git add

  • git add -A/--a- stages all changes
  • git add .- stages new files and modifications, without deletions (on the current directory and its subdirectories).
  • git add -u/--update stages modifications and deletions, without new untracked files
  • git add -A is equivalent to git add .; git add -u

git log

  • git log- retrieves information which is a log of commits
  • git log --pretty=oneline,short,medium,full,fuller,reference,email,raw,foramt:<string> and tformat:<string>
  • git log --oneline

git branch

  • To create new branches:
    • git checkout -b <branch_name>
    • git switch -c <branch_name>
  • git checkout - or git switch --to go back to the previous branch
  • git branch- list of local branches
  • git branch -a- list of all the branches (local & remote)
  • git branch -m <new_branch_name>- to rename a branch
  • git branch -d <branch_name>- to delete a branch if it has been pushed and merged with remote branch
  • git branch -D <branch_name>- to force delete a branch even if it hasn't been pushed and merged with remote branch

git diff

  • git diff- compares staging area and working directory (only shows the unstaged changes i.e not staged for next commit)
  • git diff HEAD- shows changes that are staged and unstaged since HEAD/last commit (HEAD is reference to last commit)
  • git diff --staged/--cached- shows changes between the staging area and the last commit
  • git diff <branch_1>..<branch_2>- list the change between the tips of branch 1 and 2
  • git diff <commit_1>..<commit_2>- list the changes between 2 commits

git push

  • git push origin <local_branch>:<remote_branch>- to push a different local branch to a different remote branch
  • git push --all- to push all the local branches to remote

git fetch and pull

  • git fetch <remote> <branch_name>- branch name is optional, required only if we want to fetch only a particular branch
  • git pull- combination of git fetch followed by git merge

git stash

  • git stash or git stash save or git stash -m- to stash something
  • git stash list- to list the stashes
  • git stash apply stash@{2}- to apply a stashed change
  • git stash drop stash@{2}- to delete a stashed change
  • git stash pop stash@{2}- combination of git stash apply and git stash pop
  • git stash clear- to delete all the stashed changes

Discarding changes & Undoing commits

git restore

  • git restore <file_name> or git checkout HEAD <file_name> or git checkout -- <file_name>-after making a lot of changes if I want to discard them then I just move back to HEAD
  • git restore . or git checkout .- to restore the current directory.
  • git restore --source HEAD~3 <file_name>- to select different source, by default HEAD is the source
  • git restore -S/--staged <file_name>- to unstage a staged file

git reset

  • git reset <commit_hash>- we move back to old commit but the changes we did will still be there in the working directory
  • git reset --hard <commit_hash>- moves back to old commit but changes won't be there

git revert

  • git revert <commit_hash>- it creates a new commit which undoes the latest commits which we did

NOTE: In git reset the history is lost but git revert saves the history


git tags - reference to a specific point in git history

  • git tag <tag_name>- to create a tag
  • git tag or git tag -l- list all the tags
  • git tag -l <some_wildcards>- to view tags (filter tags with some wildcards) | ex:git tag -l "*beta*"
  • git diff <tag_1> <tag_2>- to see the differences between the two tags
  • git tag -a <tag_name> -m "some message"- to create an annotated tag
  • git show <tag_name>- to see the details of the tag
  • git tag <tagname> <commit>- To tag some older commits
  • git tag -f <tagname>- to replace/move tags forcefully
  • git tag -d <tagname>- to delete tags
  • git push origin <tagname>- to push a single tag
  • git push origin --tags- to push all the tags

git reflog (reflog = reference log)

  • git reflog- records of when references were updated
    • can be found under .git/refs directory in your repository.
    • subcommands - show , expire, delete, exists

NOTE: git log only includes commits and its parent and so on whereas git reflog includes history of things ex: clones, check-outs, deleted commits, etc.

  • git reflog show HEAD- logs of the references when HEAD was changed

  • git reflog show <branch_name>- logs of the references when

  • git reflog show HEAD@{10}- shows all the reference logs from HEADs position 10 moves ago till its position at the beginning of the repository.

    • HEAD{0}- refers to the most recent entry in the HEAD reflog
  • git checkout HEAD@{2}- it takes us back where HEAD was 2 steps before (need not be same branch or can be same branch and even same commit)

  • git checkout HEAD~2- moves the HEAD back to 2 commits

  • Time based reflogs
    • git reflog master@{one.week.ago]}
    • git reflog master@{1.day.ago}
    • git reflog master@{yesterday}

Git Objects (Notes)

There are 4 types of git objects stored in the objects (/.git/objects) directory in your repository:

  1. blob - binary large object, git uses to store the contents of the files (they don't include the file name)
  2. tree - are git objects used to store the content of the directories. Trees contains pointers that can refer to blobs and to other trees
  3. commit - it combines a tree object along with information about the context that led to the current tree. Commit stores - a reference to a tree, a reference to parent commit, the author, the commiter and the commit message
  4. annotated tag - it is a pointer to a particular commit

img.png

  • A annotated tag points to a particular commit.
  • A commit points to a tree.
  • A tree can point to many blobs and other trees
  • git hash-object <file>- command that provides the hash result using SHA-1 encryption for the content of the file

Example of git hash-object command

echo "some-text" | git hash-object --stdin
  • git cat-file -p <object-hash>- To retrieve the data from a hash
  • git cat-file -t <object-hash>- tells the type of the object (blob/ tree/ commit/ annotated tag)
  • git cat-file -p main^{tree}- command for viewing tree. the syntax main^{tree} specifies the tree object that is pointed to by the tip of our branch 9in this case main).

About

This repository is a comprehensive collection of my Git knowledge, including commands, best practices, tutorials, and tips. It serves as a personal reference and a resource for anyone looking to enhance their Git skills.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published