Table of contents
Terminology
Git basics
Term | Explanation |
---|---|
Branch | A branch in Git is a lightweight movable pointer to a commit. Branches are used to isolate work and can be easily created, merged, and deleted. |
Init | Initialize a new Git repository. When executed in a directory, it sets up the necessary data structures and configuration files for version control, creating the initial commit and establishing the repository for tracking changes. |
Clone | Cloning is the process of creating a copy of a remote repository on your local machine. It establishes a connection between your local and the remote repository. |
Working with changes
Term | Explanation |
---|---|
Untracked files |
Untracked files are files in your working directory that Git is not currently
managing. Git is unaware of changes in these files.
If you enter |
Unstaged files |
Unstaged changes are modifications made to tracked files that Git is aware of
but has not yet been marked to be included in the next commit.
If you enter |
Staged files |
Staged changes are modifications that have been marked to be included in the next
commit. The changes are in the staging area (or index).
If you enter |
Committed files | Committed files are changes that have been permanently saved in a commit. A commit in Git represents a snapshot of the changes made to a repository at a specific point in time. Each commit is identified by a unique hash. |
Git concepts
Term | Explanation |
---|---|
index | The index, also known as the staging area, is a place where changes are prepared to be committed. It holds a snapshot of the content that will be part of the next commit. |
Head | HEAD is a reference to the latest commit in the currently checked-out branch. It points to the last commit you made or the tip of your current branch. |
Remote | A remote in Git is a reference to a repository on the internet or another network. It allows fetching or pushing changes to/from that repository. |
Tag | A tag is a reference that points to a specific commit, often used to mark release points. Unlike branches, tags do not change. |
Repository and branch management
Term | Explanation |
---|---|
Master | In Git, "master" is a default branch name. It's often the main development branch from which other branches are created. |
Origin | "Origin" is the default name for the remote repository from which your local repository was cloned. It's a common convention, but you can change this name. |
Fetch | Fetching in Git means retrieving changes from a remote repository without merging them. It updates the remote tracking branches. |
Pull | Pulling in Git is a combination of fetching changes and merging them. It updates the local branch with changes from the remote repository. |
Push | Pushing in Git is the process of sending committed changes to a remote repository. It updates the remote branch with local changes. |
Git workflow
Term | Explanation |
---|---|
Merge | Merging is the process of combining changes from different branches into a single branch. It brings the changes made in one branch into another. |
Commit | A commit in Git represents a snapshot of the changes made to a repository at a specific point in time. It records changes to files, and each commit is identified by a unique hash. Commits are the fundamental building blocks in Git and are used to track the history of a project. |
Push | Pushing in Git is the process of sending committed changes to a remote repository. It updates the remote branch with local changes. |
Revert | Revert is the process of undoing a commit by creating a new commit that undoes the changes made in the previous commit. |
Stash | Stashing in Git allows you to save changes that are not ready to be committed yet. It provides a way to temporarily save and set aside changes so that you can switch to a different branch or perform other actions. |
Collaboration and advanced concepts
Term | Explanation |
---|---|
Fork | Forking is a feature on platforms like GitHub. It involves creating a personal copy of someone else's project. Changes can be made in the forked repository without affecting the original project. |
Pull request | A pull request is a way to propose changes to a repository. It informs others about the changes you've pushed to a branch in your forked repository. |
Rebase | Rebase is the process of moving or combining a sequence of commits to a new base commit. |
Cherry pick | Cherry-picking in Git is the act of selecting and applying specific commits from one branch to another. It allows you to choose individual changes (commits) and apply them to a different branch, effectively picking the changes you want. It is used to maintain a clean and linear project history. |
Hooks | Initializes sample hook scripts in the .git/hooks directory. Hooks are scripts that can be triggered at different points in the Git workflow, such as before or after commits. |
Submodules | A submodule is a Git repository embedded within another Git repository. It allows you to include external repositories as a subdirectory in your project. |
Release | A release in Git often involves creating a stable version of your project and tagging it with a version number. It marks a point in the project's history where a specific set of features or changes is considered complete and ready for production use. |
Initialize/Clone
git init - Initialize the repository, it will create.git directory. Configuration will be created at ./.git/config git clone - Downloads a project with the entire history from the remote repository.
Branches
git branch - View local branches.git branch -r View remote branchesgit branch -a View local and remote branches.git branch - Create new branch.git checkout - Switch to branch to .git checkout -b issue47 - Create branchissue47 and switch to it.git checkout -b my-feature-branch origin/remote-feature-branch - Create a new local branch from the remote and switch to it (it's good practice to run beforegit fetch origin )git push -u origin ,git push -u origin HEAD ,git push --set-upstream origin my-branch-name - Push branch to remote.git branch -d - Delete branch locally.git branch -D - Delete branch locally, even if there are changes.git push origin --delete - Delete branch remotely.
Status
git status - Show repository status.
Logs
git log - Show git logs.git log --oneline - Show git logs with one line formatting.git log - Show git logs where current file present.git log --oneline - Show git logs where current file present with one line format.git log --oneline --all - Log all commits.
Difference
git diff - Show the difference.
Add to Stage
git add - Add file to stage.git add . - Add all to stage.
Commit
git commit -m " - Commit the changes with the message." git commit -a -m " - Automatically stage files and commit the changes with the message."
View commits
git checkout 2e8dd5ya - View commit with ID2e8dd5ya , to get ID entergit log --online git checkout master - Return to themaster last commit.
Tag a Release
git tag -a v1.0 -m "Stable version" - Tagging the most recent commit.git checkout v1.0 - View by tag (that we added before, important to have before-m flag).
Removing/Restoring files from working tree
git rm - Remove file from working directory, and stop tracking it.git rm -r . - Remove all files from working tree.git rm -f - Deletes a file from the working tree, removing it from the folder even if it has been recently modified.git rm --cached - Converts a tracked file into an untracked one, effectively eliminating it from Git while retaining the file in your local directory.git restore --staged - To restore staged file.
Configuration
Getting/Setting configuration
git config --list --local - Check local configurations.git config --list --global - Check global configurations.git config --global user.name "John Doe" - Set global user name.git config --global user.email "john.doe@gmail.com" - Set global email.
Configuration file level
Configuration level | Description |
---|---|
Local |
Applies exclusively to the current repository, and its configuration file is stored at
|
Global |
Affects all repositories within the user's home directory. The configuration file is
located at |
System |
Applies universally to all repositories on the machine. The configuration file
is stored at |
Stash
git stash - Stash all changes.git stash save "MyStash" - Create named stash.git stash push -m "aaa.ts | bbb.ts - changes" aaa.ts bbb.ts - Stash particular files with message, in this caseaaa.ts andbbb.ts git stash push -S -m "Only staged" - Stage only staged.git stash push --patch - Interactive Stashingy : Stash the selected change.n : Do not stash the selected change.q : Quit the patch mode.a : Stash all changes without asking for each one.d : Do not stash any changes.? : Display help for the patch mode./ : Search for a specific string in the diff.s : Split the current hunk into smaller hunks.e : Manually edit the current hunk.
git stash list - Show stash list.git stash show stash@{2} - Display stash details, such as changed files.git stash show -p stash@{2} - Display the files and the changes made in those files in the specified stashgit stash apply - Apply top stash.git stash apply stash@{5} - Apply specific stash, where5 is the index of the stash (index starting from0 ).git stash pop - Apply changes/stash and remove stash.git stash drop - Remove top stash.git stash drop - Remove stash by name.git stash clear - Clear history.git stash -- myfile.txt - Stash single file.
Reset
git reset - Resets the staging area to the most recent commit while keeping changes in the working directory.git reset - Moves the current branch tip to , resetting the staging area to match it, and leaves the working directory unchanged.git reset HEAD - Unstages by resetting it to the current commit (HEAD), affecting only the staging area.git reset --hard - Discards all changes permanently, resetting both the staging area and the working directory to the most recent commit.git clean -f - Removes untracked files forcefully from the working directory.
Undo committed changes
git revert 514fbe7 - Undo the specified commit by applying a new commit.
Note When using
Merge
git merge - merge branch into the current
Cherry pick
git cherry-pick f6243d2dd85b3504c3dcfaee3562fef1d7437a15 - get changes of commit with idf6243d2dd85b3504c3dcfaee3562fef1d7437a15
Submodules
Adding submodules
- Create two independent repositories with names
A andB - Clone A repository
git clone https://github.com/obaranovskyi/A.git - Navigate to root folder
cd A - Submodule
B repositorygit submodule add https://github.com/obaranovskyi/B (link to B repo)
Along with the repository will be added
- Push the changes:
git commit -m "Added submodule B"
git push
Getting the latest changes to submodules
git pull --recurse-submodules - Pull the changes including the submodule changes.
Or, set config to have default value: git config submodule.recurse = true
Removing submodules
- Delete the relevant line from the
.gitmodules file. - Stage the
.gitmodules changes (git add .gitmodules ). - Delete the relevant section from
.git/config . - Run
git rm --cached path_to_submodule_to_remove (no trailing slash). - Delete the submodules's
.git directory (rm -rf .git/modules/path_to_submodule_to_remove ) - Commit the superproject (
git commit -m "Removed submodule )." - Delete the now untracked submodule files (
rm -rf path_to_submodule_to_remove ).
Clone with the classic token
Create a classic token
- Click on the user icon and choose
Settings Developer Settings Personal access tokens Tokens (classic) - Click on
Generate new token - Select
Generate new token (classic) .
Use a newly generated token to clone the repository
git clone https://<your-classic-token>@github.com/<username>/<project-name>.git