
Git is a free versioning software; each project is a repository. This tool enables collaborative work on the same codebase. It is notably used by the open source platform GitHub.
Vocabulary
- Repository A place where code versions are stored. Repositories can be compared to projects.
- Commit A version of the project at a given point in time.
- Push Sends one or more commits to a repository.
- Pull Downloads commits from a repository.
- Fork A fork allows anyone with access to a repository to clone it and make their own modifications.
- Tree Contains a commit, describes the source tree structure; a tree object points to multiple parent commits.
- Branch Since each commit knows its parents, it is possible to have two teams working on two different branches (the main branch is master). This allows creating two parallel versions of the project. The two branches can then be merged to combine everyone’s work. Example: a dev branch and a production branch (master). Once a commit on the dev branch is validated, the branches are merged and the main branch gets the new features.
- Merge The fusion of two branches.
- Stash A local storage space containing modifications that have not yet been committed. Useful for example when switching branches with pending changes.
- Rebase Allows rewriting part of the commit history.
- Cherry-pick Allows adding a commit to another branch without merging the entire branch.
Essential Commands
Initialize a git project
git init
List configuration globals
git config --list
Configure username
git config --global user.name "your_name"
Configure email
git config --global user.email "[email protected]"
Status of modified files
git status
First commit
git add .
git commit -m "initial commit"
Subsequent commits
git add path_to_my_file
git commit -m "commit message"
Amend the previous commit
git commit --amend
View commit history
git log
View commit history (one line)
git log --oneline
View branches as a graph
git lg
View differences from the last commit
git diff
Restore a file to a previous commit
git checkout <commit> <file>
Revert to a previous commit (adding modified files to staging)
git reset <commit>
Revert to a previous commit (removing files from staging)
git reset <commit> --hard
Create a reverse commit
git revert <commit>
Rebase a branch
git checkout <commit>
git rebase master
Interactive rebase
git rebase -i <commit>
Interactive rebase works with a scripting system. When running the command, a new window opens where you can perform these actions on each commit:
- pick Use the commit
- rework Modify the commit message.
- edit Modify both the commit message and its content.
- squash Merge the commit with the previous one.
- fixup Like squash but allows modifying the new commit message.
- exec Execute a shell command.
Remove a file from the working directory and index
git rm filename
List branches
git branch
Create a branch
git branch my_branch_name
Switch branches
git checkout my_branch_name
Delete a branch
git branch -d hotfix
Merge dev branch changes into master
git checkout master
git merge dev
Cherry-pick a commit onto master
Cherry-pick allows merging a commit from branch 1 onto branch 2. This moves branch 1 forward with a commit from branch 2 without fetching all of branch 2’s commits.
git checkout master
git cherry-pick c90fd66
Create a stash
git stash
Stash including untracked files
git stash -u
List stashes
git stash list
View stash changes
git stash show stash@{0} [-p]
Apply a stash
git stash apply stash@{0}
Drop a stash
git stash drop stash@{0}
Apply and drop a stash
git stash pop stash@{0}
List configured remote repositories
git remote -v
Add a remote repository
git remote add remoteName https://github.com/user/repo.git
Remove a remote repository
git remote rm repoName
Fetch changes from the current branch on a remote without merging (creates a new branch)
git fetch yourRemote
Pull changes from the current branch on a remote
git pull yourRemote
Push commits to the remote repository
git push yourRemote branch
Clone a remote repository
git clone git://github.com/user/repo.git