Essential Git Commands for Managing Projects on an AWS EC2 Linux Server

Git-CheatSheet

Here are some commonly used Git commands tailored for working with a Git repository on an AWS EC2 Linux server:


Setup Commands

  1. Install Git (if not already installed):
    sudo apt update
    sudo apt install git -y
    
  2. Configure Git (Set global username and email):
    git config --global user.name "Your Name"
    git config --global user.email "[email protected]"
    
  3. Verify Git configuration:
    git config --list
    

Cloning a Repository

  1. Clone a repository:
    git clone https://github.com/username/repository.git
    
  2. Clone a private repository using SSH:
    • First, add your SSH key to the server:
      ssh-keygen -t rsa -b 4096 -C "[email protected]"
      

      Copy the public key (~/.ssh/id_rsa.pub) and add it to your GitHub account under SSH keys.

    • Then, clone using SSH:
      git clone [email protected]:username/repository.git
      

Basic Workflow Commands

  1. Check the repository status:
    git status
    
  2. Add changes to staging:
    git add .
    

    Or add specific files:

    git add file_name
    
  3. Commit changes:
    git commit -m "Your commit message"
    
  4. Push changes to the remote repository:
    git push origin branch_name
    
  5. Pull updates from the remote repository:
    git pull origin branch_name
    

Branch Management

  1. List all branches:
    git branch -a
    
  2. Create a new branch:
    git checkout -b new_branch_name
    
  3. Switch to another branch:
    git checkout branch_name
    
  4. Delete a local branch:
    git branch -d branch_name
    
  5. Push a new branch to the remote repository:
    git push origin new_branch_name
    

Remote Repository Management

  1. View remote repositories:
    git remote -v
    
  2. Add a new remote repository:
    git remote add origin https://github.com/username/repository.git
    
  3. Remove a remote repository:
    git remote remove origin
    

Miscellaneous

  1. View commit history:
    git log
    
  2. Reset changes (unstaged changes):
    git checkout -- file_name
    
  3. Undo the last commit (but keep changes staged):
    git reset --soft HEAD~1
    
  4. Undo the last commit (and discard changes):
    git reset --hard HEAD~1
    

These commands will help you manage your Git workflow on an AWS EC2 Linux server.

Leave a Reply