GENERAL GIT COMMAND
Basic Git Workflow
1. Initialize a Git Repository
To start version controlling your project, navigate to your project directory in the terminal and run:
git init
2. Stage Changes
Add files to the staging area to prepare them for committing:
git add <file> # Stage a specific file
git add . # Stage all changes in the current directory
3. Commit Changes
Commit staged changes to the local repository:
git commit -m "Your commit message here"
4. Push Changes to Remote Repository
Push committed changes to a remote repository (like GitHub):
git push origin <branch_name>
5. Pull Changes from Remote Repository
Fetch and merge changes from a remote repository into your local repository:
git pull origin <branch_name>
6. Check Status
View the status of your repository, including tracked/untracked files and changes:
git status
7. View Commit History
View a list of commits and their details:
git log
8. Create a New Branch
Create a new branch for feature development or bug fixing:
git branch <branch_name>
9. Switch Branches
Switch to a different branch:
git checkout <branch_name>
10. Merge Branches
Merge changes from one branch into another:
git merge <branch_name>
Additional Git Commands
1. Clone a Repository
Clone an existing repository from a remote server (like GitHub):
git clone <repository_url>
2. Remove Files
Remove files from the staging area and/or working directory:
git rm <file> # Remove a file from both staging area and working directory
git rm --cached <file> # Remove a file from the staging area only
3. Undo Changes
Undo changes made to files:
git checkout -- <file> # Restore a file to its state at the last commit
4. Configure Git
Set up user information for Git commits:
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
5. Inspect Changes
View the changes introduced by a specific commit:
git show <commit_hash>
6. View Remote Repositories
View the list of remote repositories associated with the current repository:
git remote -v
7. Rename and Move Files
Rename or move files within the repository:
git mv <old_file_path> <new_file_path>
8. Amend the Last Commit
Add changes to the last commit or modify its commit message:
git commit --amend
9. Create Tags
Create lightweight or annotated tags to mark specific commits:
git tag <tag_name> # Lightweight tag
git tag -a <tag_name> # Annotated tag with a message
10. Checkout Specific Commit
Check out a specific commit by its commit hash:
git checkout <commit_hash>
11. Reset Changes
Reset the repository to a specific state:
git reset --hard <commit_hash> # Reset to a specific commit
git reset --hard HEAD # Reset to the last commit
12. Fetch and Merge Remote Changes
Fetch remote changes without merging them:
git fetch <remote_name>
13. Rebase Commits
Rebase the current branch onto another branch or commit:
git rebase <branch_name>
14. Ignore Files
Exclude files or directories from version control using a .gitignore
file:
touch .gitignore # Create a .gitignore file
15. Show Differences with Previous Commit
View the changes introduced by the last commit:
git diff HEAD^ HEAD # Differences between the last and second-to-last comm
Last updated