Giter VIP home page Giter VIP logo

git-cheatsheet's Introduction

⭐ git-cheatsheet ⭐

This Repository is a guide to all the git commands you need to use in case any of these situations arise

Basic Commands ✌️

  • Clone a Repository
git init

 Turn an existing directory into a git repository

git clone https://github.com/CodeChefVIT/git-cheatsheet.git

 Replace  url with your required repository url  
  • Set username and emailId
   git config --global user.name "FIRST_NAME LAST_NAME"
   
   git config --global user.email "[email protected]"
  • Pushing to a Repository
  git status
    show modified files in working directory, staged for your next commit
  
  git add .
  git commit -m <commit message>
  git push origin master
   
   To add just one file or a set of files replace the . with your filename in first command
  
  git diff
    diff of what is changed but not staged
  git diff --staged
    diff of what is staged but not yet commited
  git commit -m “[descriptive message]”
    commit your staged content as a new commit snapshot
  
  • Change most recent Git commit message
git commit --amend
  • Check where is your Repository remote
  git remote -v

  Output should have the repository url similar to this 

  origin	https://github.com/CodeChefVIT/git-cheatsheet.git (fetch)
  origin	https://github.com/CodeChefVIT/git-cheatsheet.git (push)
  • Change Remote
git remote set-url origin <new git url>
  • Check logs
    git log 
   
   Output should be something similar 

    commit 82e2a7c46a96b3b4aaf5acbc0cbc218d118aa922
    Author: <User> <45638240+<User>@users.noreply.github.com>
    Date:   Fri May 15 14:52:32 2020 +0530

    <Commit Message>
  • Tracking changes
  git diff
    To track the changes that are yet to staged.

    git diff --staged
      To track the changes that are staged but not committed.
    
    git diff HEAD
      To track the changes after committing a file.
        
    git status
      To know the state of the files in local directory.
    
    git show
     To show all the changes made in the file for each commit
     
  • Ignoring files
    Git can ignore specified files from adding into the remote repository using     gitignore. 
    
    Create .gitignore in the project
    
     vim .gitignore 

Add the filename/directory you want to ignore by the git in the gitignore file

node_modules  

now, when you add the files it ignores node_modules directory in your project.

Branches 🌳

  • Create new branch
   git checkout -b newbranch

   Replace newbranch with your branch name
  • Check current branch
   git branch

   This should list all your branches and highlight the current branch in green
  • Switch to new branch
   git checkout newbranch
  • Push in new branch
    git add .
    git commit -m <commit message>
    git push origin <branch name>

Update forked Repository with original Repository ⌛

These steps are to be followed when your forked repository is few commits behind original repository

  - Add original repository url here 

    git remote add upstream https://github.com/whoever/whatever.git

  - Fetch all branches of the repository

    git fetch upstream

  - Make sure you are on master branch

    git checkout master

  -  Rewrite your master branch so that any commits of yours that aren not already in upstream/master are replayed on top of that other branch

    git rebase upstream/master

    This should update your forked repository with original repository

    However if you do not want to rewrite history of master branch then replace last command with this

    git merge upstream/master
    
 

Your dev branch is X commits behind and Y commits ahead of master fix

  git checkout master

  git fetch origin master
 
  git checkout dev

  git rebase origin/master

  git checkout master

  git merge --no-ff dev
  
  If you get this message  'Automatic merge failed; fix conflicts and then commit the result'
  
  Check for merge conflicts in code and fix them (master branch should have required files now)
  
  For any such message deleted in  'dev and modified in HEAD. Version HEAD of requirements.txt left in tree' 
  
  File can either be deleted or modified or kept same
  
  git pull origin master (if warning comes)
  
  add , commit , push 

Short hands

    git commit -am <message> 
      adds and commits the changes in a single command
    git push -u origin <branch name>
      use -u (upstream) for pushing your first commit changes into the remote repository
      and later on you avoid origin <branch name>

   git push 
      works fine till the last commit    
    git diff > difference.txt
      If you are feeling hard to track all the changes on console 
      above helps to writes/pipes the differences into specified file (difference.txt) and you can track the changes
      easily

GitHub CLI commands:

1. gh issue list
View and filter a repository’s open issues.

2. gh pr status
Check on the status of your pull requests.

3.  gh pr checkout
Check out pull requests locally.

4. gh pr create
Create a new pull request.

5. gh pr checks
View your pull requests’ checks.

6. gh release create
Create a new release.

7.  gh repo view
View repository READMEs.

8. gh alias set
Create a shortcut for a gh command.

git-cheatsheet's People

Contributors

0xpranjal avatar akshatvg avatar antirex avatar diptochakrabarty avatar sloorush avatar vvvk-gh avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.