Basic Git Commands
git init– Initialize a new Git repositorygit clone <repo_url>– Clone a remote repositorygit status– Check the status of your working directorygit add <file>– Stage a specific filegit add .– Stage all changesgit commit -m "commit message"– Commit changes with a messagegit commit --amend– Edit the last commit
Branching & Merging
git branch– List all branchesgit branch <branch-name>– Create a new branchgit checkout <branch-name>– Switch to another branchgit checkout -b <branch-name>– Create & switch to a new branchgit merge <branch-name>– Merge another branch into the current branchgit branch -d <branch-name>– Delete a branchgit branch -D <branch-name>– Force delete a branch
Stashing Changes
git stash– Save uncommitted changesgit stash list– Show stashed changesgit stash pop– Apply & remove the most recent stashgit stash apply– Apply the most recent stash (without removing)git stash drop– Delete the most recent stash
Undoing Changes
git restore <file>– Discard uncommitted changes in a filegit reset HEAD <file>– Unstage a filegit reset --hard– Reset everything to the last commitgit revert <commit-hash>– Create a new commit that undoes a previous commit
Viewing History
git log– View commit historygit log --oneline– Show history in one line per commitgit log --graph --oneline --all– View branch structure
Remote Repositories
git remote add origin <repo_url>– Connect to a remote repogit remote -v– List remote repositoriesgit push -u origin <branch-name>– Push a branch to a remote repogit pull origin <branch-name>– Fetch & merge changes from the remote repogit fetch– Fetch latest changes without merging
Tagging
git tag– List all tagsgit tag <tag-name>– Create a new taggit tag -a <tag-name> -m "message"– Create an annotated taggit push origin <tag-name>– Push a tag to a remote repogit push origin --tags– Push all tags
Git Configuration
git config --global user.name "Your Name"– Set usernamegit config --global user.email "[email protected]"– Set email
No Comments
Leave a comment Cancel