-
Initialize a repository:
git init
-
Clone a repository:
git clone <repository_url>
-
Add files to staging area:
git add <file_name>
-
Commit changes:
git commit -m "commit message"
-
Push changes to remote repository:
git push <remote_name> <branch_name>
-
Pull latest changes from remote:
git pull <remote_name> <branch_name>
-
Create a new branch:
git branch <branch_name>
-
Switch to a branch:
git checkout <branch_name>
-
Create a new branch and switch to it (shortcut):
git checkout -b <branch_name>
-
Merge a branch into the current branch:
git merge <branch_name>
-
Delete a branch:
git branch -d <branch_name>
-
Add a remote repository:
git remote add <remote_name> <repository_url>
-
View remote repositories:
git remote -v
-
Remove a remote repository:
git remote remove <remote_name>
-
Check the status of the repository:
git status
-
View commit history:
git log
-
View a compact commit history:
git log --oneline
-
View changes made in a specific commit:
git show <commit_hash>
-
Discard changes in a file (restore from the last commit):
git checkout -- <file_name>
-
Undo the last commit (keeping changes in the working directory):
git reset HEAD~1
-
Undo the last commit and discard changes:
git reset --hard HEAD~1
-
Create a Pull Request (PR) on GitHub website:
- Push your changes to a new branch on GitHub
- Click on "Pull Request"
- Review and submit the PR
-
Review and merge a Pull Request:
- On GitHub, go to the Pull Request page
- Review changes and discussions
- Merge the PR when ready
-
Update your local branch with changes from the PR (after it's merged):
git pull <remote_name> <branch_name>
-
Fetch changes from a remote repository (without merging):
git fetch <remote_name>
-
Push changes to a specific branch on the remote repository:
git push <remote_name> <local_branch_name>:<remote_branch_name>
-
Set up the default remote repository and branch for push and pull:
git push -u <remote_name> <local_branch_name>
-
When a merge conflict occurs, open the conflicted file and manually resolve the conflicts, then commit the changes.
-
Abort a merge or rebase (in case of conflicts):
git merge --abort git rebase --abort
-
Create an annotated tag:
git tag -a <tag_name> -m "tag message"
-
Push tags to the remote repository:
git push --tags
-
Temporarily save changes in a "stash" (useful for switching branches):
git stash save "stash message"
-
Apply the most recent stash and remove it from the stash list:
git stash pop
-
Apply a specific stash by index and remove it from the stash list:
git stash pop stash@{<index>}
- Automate workflows with GitHub Actions. Create a
.github/workflows
directory and add YAML files defining your workflows.
- Create a
.gitignore
file to exclude specific files or directories from version control.
-
Install the GitHub CLI (gh) and authenticate:
gh auth login
-
Create a new repository using the GitHub CLI:
gh repo create <repository_name> --public --description "<description>"
-
Fork a repository on GitHub website:
- Click on "Fork" button on the top-right corner of the repository page.
-
Keep your forked repository synced with the original repository:
- Add the original repository as an upstream remote.
- Fetch and merge changes from the upstream remote into your local repository.
-
Set your name and email (global configuration):
git config --global user.name "Your Name" git config --global user.email "youremail@example.com"
-
Set default editor (e.g., for commit messages):
git config --global core.editor "editor_name"
-
Rebase your current branch onto another branch:
git rebase <branch_name>
-
Resolve conflicts during a rebase:
- Open the conflicted file and manually resolve the conflicts.
- Use
git add
to stage the resolved changes. - Continue the rebase with
git rebase --continue
.
- Apply a specific commit from one branch to another:
git cherry-pick <commit_hash>
-
Undo commits by removing them from the branch history:
git reset <commit_hash>
-
Soft reset: move branch pointer to a previous commit without modifying the working directory or staging area:
git reset --soft <commit_hash>
-
Hard reset: move branch pointer and discard all changes after the specified commit:
git reset --hard <commit_hash>
- Use binary search to find the commit that introduced a bug:
git bisect start git bisect bad # Set current commit as bad git bisect good <commit_hash> # Set known good commit
-
Add a submodule to your repository:
git submodule add <repository_url> <path>
-
Clone a repository along with its submodules:
git clone --recurse-submodules <repository_url>
- Host a static website using GitHub Pages:
- Create a branch named
gh-pages
and push your static files to it. - Go to the repository's settings on GitHub, scroll down to the GitHub Pages section, and select the
gh-pages
branch as the source.
- Create a branch named
- Track large files using Git LFS:
- Install Git LFS:
git lfs install
- Track a file with LFS:
git lfs track <file_name>
- Commit and push changes as usual.
- Install Git LFS:
- Use Git hooks to automate tasks at various stages of the Git workflow. Hooks are scripts placed in the
.git/hooks
directory of a repository.
- Create custom Git aliases for frequently used commands:
git config --global alias.<alias_name> <git_command>
-
Create an issue on GitHub:
- Go to the repository on GitHub and click on the "Issues" tab.
- Click on "New Issue" and provide the necessary details.
-
Reference an issue in a commit message:
git commit -m "Commit message. Fixes #<issue_number>"