Basic Operations
| Command | Description |
|---|
git init | Initialize a new repository |
git clone <url> | Clone a repository |
git status | Check working directory status |
git add <file> | Stage a file |
git add . | Stage all changes |
git commit -m "message" | Create a commit |
git push | Push to remote |
git pull | Pull from remote |
Branch Operations
| Command | Description |
|---|
git branch | List branches |
git branch <name> | Create a new branch |
git checkout <branch> | Switch to a branch |
git checkout -b <name> | Create and switch to a new branch |
git merge <branch> | Merge a branch |
git branch -d <name> | Delete a branch |
History
| Command | Description |
|---|
git log | Show commit history |
git log --oneline | Show history in one line |
git log --graph | Show history as a graph |
git diff | Show differences |
git show <commit> | Show commit details |
Undo Operations
| Command | Description |
|---|
git reset HEAD <file> | Unstage a file |
git checkout -- <file> | Discard working directory changes |
git revert <commit> | Revert a commit (creates new commit) |
git reset --hard <commit> | Reset to a commit (dangerous) |
Remote Operations
| Command | Description |
|---|
git remote -v | List remotes |
git remote add <name> <url> | Add a remote |
git fetch | Fetch remote information |
git push -u origin <branch> | Push and set upstream branch |
Useful Aliases
[alias]
st = status
co = checkout
br = branch
ci = commit
lg = log --oneline --graph --decorate
unstage = reset HEAD --
last = log -1 HEAD
amend = commit --amend --no-edit
Troubleshooting
| Situation | Solution |
|---|
| Wrong commit message | git commit --amend -m "new message" |
| Committed by mistake | git reset --soft HEAD~1 to undo |
| Merge conflict | Edit files → git add → git commit |
| Push rejected | git pull --rebase then push again |
| Restore specific file | git checkout <commit> -- <file> |
Branch Workflow Best Practices
git checkout -b feature/user-auth
git add . && git commit -m "feat: implement user auth"
git push -u origin feature/user-auth
git checkout main && git pull
git branch -d feature/user-auth
Related Articles
← Back to list