Here are some commonly used Git commands tailored for working with a Git repository on an AWS EC2 Linux server:
Setup Commands
- Install Git (if not already installed):
sudo apt update sudo apt install git -y
- Configure Git (Set global username and email):
git config --global user.name "Your Name" git config --global user.email "[email protected]"
- Verify Git configuration:
git config --list
Cloning a Repository
- Clone a repository:
git clone https://github.com/username/repository.git
- 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
- First, add your SSH key to the server:
Basic Workflow Commands
- Check the repository status:
git status
- Add changes to staging:
git add .
Or add specific files:
git add file_name
- Commit changes:
git commit -m "Your commit message"
- Push changes to the remote repository:
git push origin branch_name
- Pull updates from the remote repository:
git pull origin branch_name
Branch Management
- List all branches:
git branch -a
- Create a new branch:
git checkout -b new_branch_name
- Switch to another branch:
git checkout branch_name
- Delete a local branch:
git branch -d branch_name
- Push a new branch to the remote repository:
git push origin new_branch_name
Remote Repository Management
- View remote repositories:
git remote -v
- Add a new remote repository:
git remote add origin https://github.com/username/repository.git
- Remove a remote repository:
git remote remove origin
Miscellaneous
- View commit history:
git log
- Reset changes (unstaged changes):
git checkout -- file_name
- Undo the last commit (but keep changes staged):
git reset --soft HEAD~1
- 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.