Post

GitHub Commands

Git and GitHub Commands Cheatsheet

Login to GitHub

  • most common problem is login in to multiple github accounts in one system. To solve this problem we can use github cli or token

  • Install Github Cli

    • On Linux:
      1
      2
      
      sudo apt-add-repository -u https://cli.github.com/packages
      sudo apt install gh
      
    • On Mac:
      1
      
      brew install gh
      
    • On windows:
      1
      
      winget install gh
      

Login to GitHub with cli

  • Now login to your github account using github cli
    1
    2
    
    gh auth login
    follow instructions
    
    • login with web browser
      1
      
      gh auth login --web
      
    • login with token
      1
      
      gh auth login --with-token [your-token]
      

Without the the use of github cli

  • Generate a Personal Access Token:

    1
    2
    3
    4
    
    Go to your GitHub account settings.
    Navigate to "Developer settings" > "Personal access tokens."
    Click on "Generate token" and follow the instructions to create a new token.
    
    
  • Open your terminal and use the following command to set up your GitHub username and token:

    1
    
    git clone https://<your-token>@github.com/<username>/<repository>.git
    

Git Commands

  • Initializes a new Git repository
    1
    
     git init
    
  • Creates a copy of a remote repository
    1
    
    git clone [repository_url]  
    
  • add multiple files

    1
    
    git add file1 file2 file3
    
  • add all files in the current directory
    1
    
    git .
    
  • Records the changes made to the files
    1
    
    git commit -m "commit message"
    
  • Modify your most recent commit
    1
    2
    
    git commit --amend
    git commit --am "Your message"
    
  • Check repository status
    1
    
    git status
    
  • Sends the committed changes of the local repository to a remote repository
    1
    
    git push
    
  • Fetches and merges changes from the remote repository to the local repository
    1
    
    git pull
    
  • Lists all the branches in the repository
    1
    
    git branch
    
  • Create a new branch
    1
    
    git branch [branch_name]
    
  • Switch to an existing branch
    1
    
     git checkout [branch_name]
    
  • create and checkout in one command

    1
    
    git checkout -b [branch_name]
    
  • Delete a local branch
    1
    
    git branch --d [branch_name]
    
  • Delete a remote branch
    1
    
    git push origin --d [remote_branch_name]
    
  • Merge a branch into the active branch

    1
    
    git merge [branch_name]
    
  • Rename a branch
    1
    
    git branch -m [new_branch_name]
    
  • View commit history
    1
    
    git log
    
  • List remote repositories
    1
    
    git remote -v
    

GitHub Commands

  • Adds a remote repository
    1
    
    git remote add origin [url]
    
  • View remote repository
    1
    
    git remote show origin 
    
  • Remove remote repository
    1
    
    git remote rm origin
    
  • Changes the URL of an existing remote repository
    1
    
    git remote set-url origin [url]
    
  • Pushes a branch to your remote repository
    1
    
    git push -u origin [branch]
    
  • Deletes a remote branch
    1
    
    git push origin --delete [branch_name]
    
  • Pulls remote branch
    1
    
    git pull origin [branch_name]
    
  • View all remote branches
    1
    
    git branch -r
    
  • Check differences between branches
    1
    
    git diff [branch1] [branch2]
    
  • Undo the last commit (while keeping changes)
    1
    
    git reset HEAD^
    
  • Discard changes in the working directory for a specific file
    1
    
    git checkout -- [file]
    
  • Show changes made in a specific commit
    1
    
    git show [commit_hash]
    
  • Create a new branch and switch to it
    1
    
    git checkout -b [new_branch]
    
  • Rename a branch
    1
    
    git branch -m [new_branch_name]
    
  • Undo the last commit and remove changes
    1
    
    git reset --hard HEAD^
    
  • View all remote branches
    1
    
    git branch -r
    
This post is licensed under CC BY 4.0 by the author.