Git Commands
Git Setup
-
Configure Git for the first time:
git config --global user.name "Your Name"
git config --global user.email "youremail@domain.com" -
Check your configuration:
git config --list
Starting with Git
-
Initialize a new Git repository:
git init -
Clone an existing repository:
git clone https://github.com/username/repository.git
Basic Git Commands
-
Add changes to staging area:
git add filenameor add all changes:
git add . -
Commit changes to the repository:
git commit -m "Commit message" -
View the commit history:
git log -
Check the status of your files:
git status
Branching and Merging
-
Create a new branch:
git branch branch_name -
Switch to a branch:
git checkout branch_nameor create and switch in one command:
git checkout -b branch_name -
Merge a branch into the current branch:
git merge branch_name -
Delete a branch:
git branch -d branch_name
Remote Repositories
-
View remote repositories:
git remote -v -
Add a remote repository:
git remote add origin https://github.com/username/repository.git -
Push changes to the remote repository:
git push origin masteror push a specific branch:
git push origin branch_name -
Pull changes from the remote repository:
git pull origin master
Miscellaneous
-
Stash changes:
git stashApply stashed changes:
git stash pop -
Reset changes: Reset to the last commit, discarding all changes:
git reset --hardUnstage changes:
git reset
-
Viewing Changes
- Show modified files:
git diff - Show changes in staged files:
git diff --cached
- Show modified files:
-
Undoing Changes
- Undo the last commit, keeping changes:
git reset --soft HEAD~1 - Revert a commit by creating a new commit:
git revert commit_hash
- Undo the last commit, keeping changes:
-
Working with Remotes
- Fetch changes from remote without merging:
git fetch origin - Set up a new remote:
git remote add remote_name remote_url - Remove a remote:
git remote remove remote_name
- Fetch changes from remote without merging:
-
Tags
- List all tags:
git tag - Create a new tag:
git tag -a tag_name -m "tag message" - Push tags to remote:
git push origin --tags
- List all tags:
-
Advanced Merging
- Abort a conflicting merge:
git merge --abort - Use the 'theirs' strategy during a merge:
git checkout --theirs filename - Use the 'ours' strategy during a merge:
git checkout --ours filename
- Abort a conflicting merge:
-
Cleaning Up
- Clean untracked files (dry run):
git clean -n - Clean untracked files for real:
git clean -f
- Clean untracked files (dry run):
-
Stashing
- List stashes:
git stash list - Stash including untracked files:
git stash -u - Apply a specific stash:
git stash apply stash@{n}
- List stashes:
-
Inspecting & Comparing
- Show the commit log as a graph:
git log --graph - Show changes over time for a specific file:
git log -p filename - Compare branches:
git diff branch1..branch2
- Show the commit log as a graph:
-
Override Global Git Username and Email for a Specific Repository
- Override Global Git Username
git config user.name "NewGitHubUsername" - Override Global Git email
git config user.email "new-email@example.com" - Verify the changes
git config --get user.name
git config --get user.email
- Override Global Git Username
Visual representation of the commands
If you have enjoyed this post, please consider buying me a coffee ☕ to help me keep writing!