Initializing a Repository
git init
Initialize a new Git repository.
Staging Changes
git add
Stage changes for the next commit.
git add .- Stage all changes.-
git add <filename.txt>- Stage changes for a specific file. -
git add <filename1.txt> <filename2.txt>- Stage changes for multiple files. -
git add subfolder/- Stage changes for all files in a specific subfolder.
Committing Changes
git commit -m "message"
Commit staged changes with a descriptive message.
Viewing Repository Status
git status
Check the status of files in the repository (staged, unstaged, etc).
Viewing Commit History
git log
View details of all commits.
git checkout <id> - Move to a specific commit
temporarily.
git checkout main - Move back to the main branch.
Reverting Changes
git revert <id>
Create a new commit to revert changes made in a specific commit.
Exiting Vim Editor
:wq- Save and exit Vim.q- Exit the log.i- Insert in Vim editor.
Resetting Changes
git reset --hard <id>
Undo changes by deleting commits since <id> (caution: changes history).
Ignoring Files
.gitignore
Specify files and folders to ignore.
Branching
Creating and Managing Branches
git branch <name>- Create a new branch.git branch- List all branches.-
git checkout <branchName>- Switch to another branch. -
git checkout -b <branchName>- Create and switch to a new branch.
Merging Branches
git merge <name> - Merge branches.
Deleting Branches
git branch -D <branchName> - Delete a branch.
Remote Repository Management
Connecting to a Remote
-
git remote add origin <url>- Link local repository to a remote repository. -
git remote- List all connected remote repositories. -
git remote get-url <origin>- Get the URL of a remote repository.
Pushing Changes
-
git push origin main- Push commits from local main branch to remote main branch. -
git push --set-upstream origin master- Set upstream and push directly afterwards.
Pulling Changes
git pull - Pull changes from the remote repository.
Configuration
Setting User Information
git config --global user.name "Username"git config --global user.email "Email"
Cloning a Repository
git clone <url>
Clone a remote repository to the local machine.
Collaboration and Workflow
- Add team members as collaborators to allow direct code changes.
- Enable branch protection on the main branch to require pull requests for changes.