Git
Install¶
Install git using homebrew with the following command:
Configuration¶
Set username¶
Set email¶
Set credential helper¶
This will store your credentials in a plain text file at ~/.git-credentials
.
Usage¶
Init¶
Initialize got tracking inside current directory
Initialize git tracking inside a new directory
Clone¶
Clone a repository
Clone only a specific branch
Clone a repository to a specific directory
Branch¶
Create a new branch
Create a new branch and switch to it
Switch to a branch
Delete a branch
Delete a branch forcefully
Merge a branch
Merge a branch with a message
Add¶
Add a file to staging area
Add all files to staging area
Stage all files except a specific file
git add -u
git git reset HEAD main/dontcheckmein.txt #or
git restore --staged main/dontcheckmein.txt
Remove a file from staging area
Commit¶
See the changes in the staging area
Saving the staged changes with a message
Saving the staged changes with a message and adding all changes
Edit the last commit message
Saving¶
Saving stage and unstaged changes
Saving stage and unstaged changes with a message
Saving stage and unstaged changes with a message and include untracked files
Apply the last stash
Apply a specific stash
Apply the last stash and remove it from the list
Remove the last stash
Remove a specific stash
Diff¶
See the changes between the working directory and the staging area
See the changes between 2 commits
Push to a previous commit¶
There are 2 ways of doing this
Reset¶
Use with caution
Use this wisely as this would manipulate the git history and could very well jeopardise everything for you.
--soft
- Moves the HEAD to the commitId and keeps the changes in the staging area--mixed
- Moves the HEAD to the commitId and keeps the changes in the working directory--hard
- Moves the HEAD to the commitId and discards all changes--merge
- Moves the HEAD to the commitId and keeps the changes in the staging area and working directory--keep
- Moves the HEAD to the commitId and keeps the changes in the working directory
Read more
If you wish to reset the HEAD
to the main
branch, you can use the following command:
Revert¶
You can use the revert
command to revert the changes made in a commit. This will create a new commit with the changes reverted. This would be an idle case when you want to keep the git history intact.
The --no-commit
flag will revert the changes but not commit them. You can then commit the changes and push them to the remote repository.
Make sure that the order of the commitId is in the reverse order of the commits you want to revert. Otherwise, you will get a conflict.
force
vs force-with-lease
¶
--force
- This will force push the changes to the remote repository alterting the git history no matter what--force-with-lease
- This will force push the changes to the remote repository only if the remote branch is in the same state as the local branch. If there are any changes in the remote branch, the push will be rejected.
Removing sensitive data from a repository¶
It is very important when you work for an organization that you do not leak any sensitive information. If you have accidentally pushed any sensitive information to a repository, you can remove it by referring this GitHub Documentation
Working with feature branches¶
Ideally when working with feature branches you will get into a situtation where you have to merge the changes from the main branch to your feature branch. You can do this by:
- Merge - This will merge the changes from the main branch to your feature branch and create a new commit
- Rebase with fast forward - This will rebase the changes from the main branch to your feature branch and not create a new commit. This will make the git history linear
- Squash - This will squash all the commits from the main branch to your feature branch into a single commit. Note this will create a new commit. We would use this when you want to keep all the changes together in a single commit.
Read more
Checkout this video by ByteByteGo on Git MERGE vs REBASE
When you rebase, you might encounter merge conflicts and these conflicts can re-appear even after you resolve them. This is because the changes are being applied on top of the changes from the main branch. You can remember the merge changes by setting up the git configuration as follows:
GitHub via SSH¶
Add SSH key to your GitHub account