- Configure user information for all repositories:
git config --global user.name "Your Name" git config --global user.email "your_email@example.com"
- Initialize a new Git repository:
git init
- Clone an existing repository:
git clone <repository_url>
- Create a new repository on GitHub:
- Go to GitHub and log in to your account.
- Click on the "+" icon in the upper right corner and select "New repository".
- Enter the repository name and description.
- Choose to make it public or private.
- Click "Create repository".
- Initialize the repository:
git init
- Add the remote repository:
git remote add origin <repository_url>
- Add files to the staging area:
git add <file> git add . # Add all files
- Commit the files:
git commit -m "Initial commit" - Push the changes to the remote repository:
git push -u origin main
- Check the status of your repository:
git status
- Add files to the staging area:
git add <file> git add . # Add all files
- Commit changes:
git commit -m "Commit message" - Amend the last commit:
git commit --amend
- Create a new branch:
git branch <branch_name>
- Switch to a branch:
git checkout <branch_name>
- Create and switch to a new branch:
git checkout -b <branch_name>
- Merge a branch into the current branch:
git merge <branch_name>
- Delete a branch:
git branch -d <branch_name>
- Delete a branch (force):
git branch -D <branch_name>
- Add a remote repository:
git remote add origin <repository_url>
- Fetch from the remote repository:
git fetch
- Push changes to the remote repository:
git push origin <branch_name>
- Pull changes from the remote repository:
git pull
- Set the remote branch for the current branch:
git branch --set-upstream-to=origin/<branch_name>
- Show commit history:
git log
- Show commit history with diffs:
git log -p
- Show a specific commit:
git show <commit_hash>
- Compare branches:
git diff <branch_name>
- Compare staged changes:
git diff --staged
- Unstage a file:
git reset <file>
- Revert a commit (create a new commit that undoes changes):
git revert <commit_hash>
- Reset to a specific commit:
git reset --hard <commit_hash>
- Stash changes:
git stash
- Apply stashed changes:
git stash apply
- List stashes:
git stash list
- Create a tag:
git tag <tag_name>
- Push tags to remote:
git push origin --tags
- Delete a local tag:
git tag -d <tag_name>
- Delete a remote tag:
git push origin --delete tag <tag_name>
- Rebase a branch:
git rebase <branch_name>
- Interactive rebase:
git rebase -i <commit_hash>
- Cherry-pick a commit:
git cherry-pick <commit_hash>
- Fork a repository:
- Go to the repository page on GitHub/GitLab and click on the "Fork" button.
- Create a pull request:
- Go to your forked repository, switch to the branch you want to merge, and click on the "New pull request" button.
- Remove untracked files:
git clean -f
- Remove untracked files and directories:
git clean -fd
- Show the summary of changes:
git status git diff git log
This cheat sheet should cover most of the commands you'll need to manage your Git repositories effectively.