Certainly! Below are some of the most commonly used basic Git commands, along with brief explanations of what they do:
Git Commands – Configuration
- Configure Git with your name and email address
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
Creating Repositories
2. Initialize a new Git repository
git init
This command creates a new Git repository in the current directory.
3. Clone an existing repository
git clone https://github.com/user/repo.git
This command copies an existing repository from a URL to your local machine.
Basic Snapshotting
4. Check the status of your files
git status
This command shows the status of changes as untracked, modified, or staged.
5. Add files to the staging area
git add <file> git add .
This command adds a file or all files (using .) to the staging area.
6. Commit changes
git commit -m "Commit message"
This command records the changes in the repository with a descriptive message.
7. View commit history
git log
This command shows the commit history for the repository.
Branching and Merging
8. Create a new branch
git branch <branch-name>
This command creates a new branch.
9. Switch to a branch
git checkout <branch-name>
This command switches to the specified branch.
10. Create and switch to a new branch
git checkout -b <branch-name>
This command creates a new branch and switches to it.
11. Merge a branch into the current branch
git merge <branch-name>
This command merges the specified branch into the current branch.
Remote Repositories
12. Add a remote repository
git remote add origin https://github.com/user/repo.git
This command adds a remote repository.
13. View remote repositories
git remote -v
This command lists remote repositories associated with the local repository.
14. Fetch changes from a remote repository
git fetch origin
This command fetches changes from the remote repository but does not merge them.
15. Pull changes from a remote repository
git pull origin <branch-name>
This command fetches and merges changes from the remote branch into the current branch.
16. Push changes to a remote repository
git push origin <branch-name>
This command pushes your local branch commits to the remote repository.
Undoing Changes
17. Remove files from the staging area
git reset <file>
This command removes a file from the staging area but keeps the file changes.
18. Undo the last commit
git revert <commit-id
This command creates a new commit that undoes the changes from a specified commit.
19. Revert all changes in the working directory to the last commit
git reset --hard HEAD
This command resets the index and working directory to the last commit’s state.
Additional Useful Commands
20. Show changes between commits, commit and working tree, etc.
git diff
This command shows changes between commits, commit and working tree, etc.
21. Show commit history in a more compact format
git log --oneline
This command shows the commit history in a compact format.
22. Stash changes in a dirty working directory
git stash
This command stashes your local modifications to a new stash and reverts your working
directory to match the HEAD commit.
23. Apply stashed changes
git stash apply
This command applies the stashed changes back to the working directory.
By familiarizing yourself with these basic Git commands, you can effectively manage version control for your projects.