Configure Git
How to configure Git
# create ssh key
ssh-keygen -t rsa -b 4096 -C "[email protected]"
# set permissions to private
chmod 400
# configure global git settings
# set key, name and email
git config --global http.sslKey path/to/company.key
git config --global user.name "John Doe"
git config --global user.email "[email protected]"
# check git config
# get global config
git config --global -l
# get local config
git config -l
Git Basic Operations
How to initialize git in your project folder
cd my-project
git init
How to add files to the staging area
git add <file-name>
# OR all changed files
git add .
How to commit changes from staging area
git commit -m 'your message goes here'
How to list all git commits
# exit by clicking `q`
git log
How to remove tracked files
git rm <file-name>
git commit -m "file deleted"
Check the current status of the working directory
git status
How to store local changes to stash area
git stash
# apply changes from the stash
git stash apply
# apply and remove changes from the stash
git stash pop
How to revert the changes from a commit
creates a new commit with the opposite changes of the reverted commit
git revert [hash]
Ignore Files
How to exclude files from tracking with .gitignore file
./idea
./vendor
.DS_Store
**/.DS_Store
.env*
node_modules
dist
*.log
How to stop a file or folder from being tracked anymore
# skips the working tree changes for a file/folder, prevents committing changes
git update-index --skip-worktree <path-name>
OR
# assume unchanged but still allow committing changes
git update-index --assume-unchanged <path-name>
OR
# !!! will be deleted from other people's local repos
# they have to get and manage these files by themselves
git rm --cached <file-name>
git rm -r --cached <folder-name>
How to start again tracking disabled file or folder
git update-index --no-skip-worktree <path-name>
# OR
git update-index --no-assume-unchanged <path-name>
Branches
How to create a new branch
git branch <branch-name>
# OR create a branch and check it out
git checkout -b <branch-name>
How to change the current branch
git checkout <branch-name>
# OR
git switch <branch-name>
How to list all branches
# list local branches
git branch
# list local + remote branches
git branch -a
# list remote branches
git branch -r
How to delete branches
# you cannot delete the current branch
git branch -d <branch-name>
# OR force delete if there are un-merged changes
git branch -D <branch-name>
How to merge branches
# merge with local branch
git merge <branch-name>
# merge with a remote branch
git merge origin/<remote-branch-name>
# get all the commits from the branch and add them to the staging area
git merge --squash <branch-name>
# you manually have to commit all the changes
git commit -m "Merge changes from the feature branch"
# enforce git to use recursive merge
git merge --no-ff <your-branch>
Repositories
How to clone remote repository
git clone <repository-url>
How to push local repository to remote repository
# origin is the default alias of the remote repository
git remote add origin <url-of-repository>
# rename branch to main
git branch -M main
# push
git push origin main
How to fetch remote changes
git fetch
How to pull remote changes into the local branch
# fetch and merge a remote branch into our local branch
# alternative - git fetch; git merge origin/<remote-branch>
git pull origin <remote-branch>
Local tracking branch
create a new local branch that tracks an existing remote branch
git branch --track <branch-name> origin/<branch-name>
# switch to this branch
git switch <branch-name>
# git pull / git push
Upstream
push a local branch to a remote repository and establish tracking
git push origin -u <branch-name>
How to delete remote branches
git push origin --delete <remote-branch-name>
# list remote branches
git ls-remote
How to reset remote branches
# first reset local branch 1 commit back
git reset --hard HEAD~1
# then push with --force changes to remote branch
git push --force origin <branch-name>
Advanced Git Operations
How to return files to a previous commit
HEAD is pointing to commit e467..
# temporarily go to previous commits - git checkout [hash]
git checkout e487a88531e323ea8fd1005bdb19a1f1d7f45ed0
# return back to master branch
git checkout master
How to restore un-staged file
reset changes to the original git content
# discard uncommitted changes in a file
git restore <file-name>
# OR restore all
git restore .
# OR old-fashioned way
git checkout <file-name>
How to reset a file content to an older version using commit hash
git restore --source [hash] [filename]
How to restore a staged file (un-stage a file)
revert changes to the original git content
# inversion of git add <file-name>
git restore --staged <file-name>
How to remove the untracked files from the working directory
# List the files for deletion
git clean -dn
# Force delete the files
git clean -df
How to reset to hash commit and forget all commits after that
# delete commit by hash + forget all new commits after hash
git reset --hard [hash]
How to reset to a specified commit and preserve the changes in the working directory.
# resetting to old version - rollback
# forget all new commits after hash
# keep changes from these commits unstaged
git reset --mixed [hash]
How to remove a commit not pushed on remote server yet
# delete the last commit + rollback the changes from the last commit
git reset HEAD~1
# delete the last commit + don't rollback the changes
git reset --hard HEAD~<number of commits to roll back>
How to remove a commit after it is already pushed on the remote server
# delete the last commit + don't keep changes locally
git reset --hard HEAD~1
# push to server with --force
git push --force
How to rebase feature branch on master
# rebase the current branch on master
# applying all the commits on the master, which are missing on the current branch
git rebase master
How to copy commit to another branch with cherry-pick
# go to branch you want to pick commit
git checkout fix-branch
# find the desired commit
git log
# go to master branch
git checkout master
# copy commit hash with cherry-pick
git cherry-pick aa19f7bbe1e2ca4f4e7add839edc007e07682d47
# push changes to master
git push
Resolving conflicts practices
How to resolve git merge conflicts on local machine
the merging developer has to resolve the conflicts make your changes in the feature branch pull the master branch (you want to merge with) on the feature branch
git pull origin master
fix the conflicts there commit fixed conflicts to feature branch merge feature to master branch always pull the latest changes before you push your branch up to the remote to resolve the conflicts in your local machine.
How to resolve git merge conflicts on remote
if you push your branch
git push -u origin feature1
you make PR and merge it to the master on the Repository server (Github) if you get a merge conflicts error
# go to local checkout branch
git checkout feature1
# pull origin master to your feature1 branch
git pull origin master
you can resolve the conflict on GitHub but it’s not a good practice resolve the conflicts on your local machine then commit and merge the branches again
Git terms explained
Git merge types
- fast-forward merge
- master branch didn’t receive any new commits since the feature branch was created
- git only include new commits from feature branch to master
- without creating a new merge commit recursive merge
- if both branches have received new commits
- git finds the common starting point for both branches and starts applying the changes
- it creates a new merge commit
- can be manually enforced by specifying no fast forward
git merge --no-ff <your-branch>
Differences between merge and rebase
- merge
- creates non-linear commit history by additional merge commits
- doesn’t change the real history of git branches
- easier to understand and debug the commit history in collaborative environments.
- conflicts may arise when multiple branches have diverged rebase linear commit history without additional merge commits alters the real commit history and makes it more challenging to track changes reduces the number of merge conflicts, they are resolved on rebasing step rebasing should be avoided if the branch is a shared repository, as it can lead to conflicts for the other team members
Git Snippets
How to list all changed files for specified period of time
git whatchanged --since '01/01/2021' --until '05/22/2021' --oneline --name-only --pretty=format: | sort | uniq