Skip to content

Instantly share code, notes, and snippets.

@Keshav13142
Last active April 4, 2024 10:28
Show Gist options
  • Save Keshav13142/c2004a6cc87cacb3dbbba917949f8bba to your computer and use it in GitHub Desktop.
Save Keshav13142/c2004a6cc87cacb3dbbba917949f8bba to your computer and use it in GitHub Desktop.
Git basics

Git basics

  • Setup Git

    • Install git
    • Configure name, email for commits
       git config --global user.name "Your name"
       git config --global user.email name@example.com
    • Create ssh keys using the ssh-keygen command
    • Add the public key to you Github account
  • Intialize a empty git repository

    git init
  • Add files to track

    git add <file_name> # add a single file
    git add <folder_name> # add a folder
    git add . # add everything under current directory
  • Commiting changes

    git commit -m "Commit message"
  • Ignoring files

    • Create a .gitignore file and add files/folders or patterns to exclude
    • Example
       echo .env > .gitignore
  • Branches

    git branch # list all local branches
    git branch -a # list local and remote branches
    git checkout <branch_name> # checkout existing branch
    git checkout -b <branch_name> # create a new branch and checkout
  • Common ways to integrate changes between different branches

  • Clone an existing remote repository

     git clone <repo_url>
  • Configure a remote repository in an existing local git repository

     git remote add <remote_name> <remote_url>
  • Pull changes from a remote repository

     git pull <remote_name> <branch_name> # push to a specific branch
  • Push changes from a remote repository

     git push <remote_name> <branch_name>
  • Stash changes

    git stash # stash all local changes
    git stash pop # restore stashed content (might cause conflicts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment