/Git-Notes

Contains most commonly used git commands


Git-Notes

I like to maintain a cheatsheet of git commands which I frequently use. The repo contains the most used commands and a small info about them which will improve your productivity and save precious hours by randomly going through stackoverflow questions.

Feel free to reach out to me! 😊
Instagram || Twitter || LinkedIn || Blog

Contribution

This repository is contribution friendly😃. If you would like to improve or add some useful commands, your contribution is welcome!


Table Of Contents:

  1. Awesome place to learn git
  2. Basic Terminologies
  3. Baisc Flow
  4. Git Branch
  5. Git Stash
  6. Miscellaneous
  7. Delete Branch
  8. Open Source Contribution

Awesome places to learn git

  1. GitHub Learning Lab
  2. Git Started with GitHub
  3. Version Control with Git
  4. Atlassian Git Tutorial
  5. Lean Git Branching

Basic Terminologies

  1. git: Git is a distributed version-control system for tracking changes in source code during software development.
  2. git clone: To download an existing git repository on your local machine.
  3. git fetch: It only downloads the new data from remote repository and does not integrate that data into working files.
  4. git pull: It downloads the new data as well as merges it into the existing files.

Basic flow

  1. Adding git to a folder(Execute inside the project folder)
    git init

  2. Adding and commiting project files
    git add .
    git commit -m "commit message"

  3. Create a README.md file

  4. Create a remote repository on Github/Gitlab/Bitbucket(Do not create README.md file here)

  5. Linking your local repository to your remote repository
    git remote add origin $URL_TO_YOUR_REMOTE_REPOSITORY

  6. Pushing local files to remote repository
    git push origin master

  7. To fetch any changes made on remote repository to local repository
    git pull origin master

Git Branch Commands

  1. Get list of branches on local
    git branch -a

  2. Get list of branches on remote
    git branch -r

  3. Create a branch
    git checkout -b $BRANCH_NAME
    git push origin $BRANCH_NAME

  4. Get list of only local branches
    git branch -vv | cut -c 3- | awk '$3 !~/\[/ { print $1 }'

  5. Display commit logs
    git log --oneline --graph --decorate --all

  6. Rename a local branch
    i. Renaming the current branch:
    git branch -m $NEW_BRANCH_NAME
    ii. Renaming some other branch:
    git branch -m $OLD_BRANCH_NAME $NEW_BRANCH_NAME

  7. Rename a remote branch
    i. First complete the above two steps.
    ii. Delete the old-name branch and push the new-name local branch:
    git push origin :$OLD_BRANCH_NAME $NEW_BRANCH_NAME
    iii. Reset the upstream branch for new-name local branch
    git push origin -u $NEW_BRANCH_NAME

  8. Delete a local branch
    git branch -d $BRANCH_NAME
    git branch -D $BRANCH_NAME

    -d stands for --delete, which will delete the local branch, only if you have already pushed and merged it with your remote branches.
    -D stands for --delete --force, which deletes the branch regardless of its push and merge status.

Git Stash Commands

  1. Saving uncommited changes(both staged and unstaged) and making the working branch clean
    git stash

  2. Saving with a message
    git stash save $MESSAGE

  3. Checkout all the stash in your project
    git stash list

  4. Pasting the stash changes and remove it from list
    git stash pop

  5. Apply ths stash changes and also keep them in the list
    git stash apply

  6. Creating a branch from stash
    git stash branch $BRANCH_NAME

  7. Delete all your stash from the project
    git stash clear

  8. Remove a particular stash from the list
    git stash drop $STASH_ID

Miscellaneous

  1. Discarding unstaged changes
    git checkout -- .

  2. Checkout conflict files
    git diff --name-status --diff-filter=U

Delete Branch

  1. Delete Local Branch
    git branch -d $BRANCH_NAME

  2. Delete Global Branch
    git push origin --delete $BRANCH_NAME

Open Source Contributions

  1. Fork the desired repository and clone that fork repo to your local machine.

  2. Fetching changes from main repository and merging them into your local copy.
    i. git remote add upstream $URL_TO_YOUR_ORIGIN_REPOSITORY
    ii. git fetch upstream
    iii. git merge upstream/master
    iv. Now push these change to your remote repository
    git push