Git is a distributed version control system (VCS) used to track changes in source code and manage projects over time.
Git allows multiple people to collaborate on a project, manage different versions of files, and revert to previous versions when needed.
Key Git Concepts:
- Repository (Repo): A directory containing all the project files and the history of changes.
- Local Repository: The repository on your local machine.
- Remote Repository: A repository hosted on a server, like GitHub, GitLab, etc.
- Pull: Fetching changes from a remote repository to the local repository.
- Push: Sending your local changes to a remote repository.
- Staging Area: A space where files are added before committing them.
- Commit A snapshot of the code at a specific point in time.
- Branch: A parallel version of the project, allowing for experimentation without affecting the main codebase.
- Merge: Combining changes from different branches into one.
Common Git Commands
Basic Commands
Initialize a Git repository
Navigate to your local directory using the command line and then enter the below command to initialize the Git repository in the directory.
git initClone a repository
git clone <repository_url>Check the repository status
git statusAdd files to the staging area
Files need to be moved to staging area before comiting.
git add <filename> // Add 1 file to staging area
git add . //Add all files in directory to staging area
Commit Changes
git commit -m "Commit message"View commit history
git logBranching & Merging
Create a new branch
git branch <branch_name>Switch to another branch
git checkout <branch_name>Create and switch to a new branch
git checkout -b <branch_name>Merge a branch into the current branch
git merge <branch_name>Delete a branch
git branch -d <branch_name>Working with Remote Repositories
View remote repositories
git remote -vAdd a remote repository
git remote add <remote_name> <remote_url>Push changes to a remote repository
git push <remote_name> <branch_name>Pull changes from a remote repository
git pull <remote_name> <branch_name>Fetch changes from a remote repository
git fetch <remote_name>Undoing Changes
Undo changes in a file (restore to the last commit)
git checkout -- <filename>Unstage files from the staging area
git reset <filename>Amend the last commit
Modifies previous commit (e.g. commit message or add files to commit)
git commit --amendRevert a commit
git revertTagging and Releases
Create a tag
git tag <tag_name>Push tags to a remote repository
git push origin <tag_name> Advanced Commands
Rebase a branch
git rebase <branch_name>Squash commits
git rebase -i HEAD~<number_of_commits>Git Configuration Commands
Set username globally
git config --global user.name "Your Name"Set email globally
git config --global user.email "you@example.com"Set default text editor
git config --global core.editor "editor_name"
Additional Notes:
Git Stash: Temporary storage for changes not ready to commit.
git stash
git stash popGit Ignore: Use .gitignore file to exclude files from being tracked.
