Here are the Git commands that I use most often.
Setup SSH Key
- List all available SSH keys on the computer
ls -al ~/.ssh- If none found, you can generate one with (replace the email with your own)
ssh-keygen -t rsa -b 4096 -C "you@example.com"- Run SSH agent on the background
eval "$(ssh-agent -s)"- Add SSH key to SSH agent, so you don’t have to type the passphrase over and over again
ssh-add ~/.ssh/id_rsa- Copy SSH key to the clipboard
pbcopy < ~/.ssh/id_rsa.pub# Orcat ~/.ssh/id_rsa.pubBasic Branching
- Create a new branch
git checkout -b branch_name- Make the changes and push to the origin
git push -u origin branch_name- If you want to pull changes from master to branch
git rebase master- Make the changes and switch back to the master
git checkout master- Merge the changes
git merge branch_name- Delete the branch
git branch -d branch_name- Delete the remote branch
git push origin --delete branch_nameTagging
# Create a taggit tag -a v1.0 -m "Stable version 1.0"git tag -a v1.0.0 -m ":bookmark: First stable version 1.0.0"git tag -a v1.0.1 -m ":bookmark: Tag patch version 1.0.1"
git tag -s v1.0.0 -m ":bookmark: First stable version 1.0.0"git tag -s v1.0.1 -m ":bookmark: Patch version 1.0.1"
# Push all tagsgit push origin --tags
# Push specific taggit push origin v1.0Ignore Already Tracked Folders or Files
- Add file / folder to the
.gitignorefile - Then run the following commands:
git rm -r --cached PATH/TO/FOLDERgit add .git commit -m "Remove untracked files."Revert to Previous Commit, Delete Any Uncommitted Changes
Run the following command to revert back to the last commit. Where HEAD is the last commit in current branch.
git reset --hard HEADgit clean -fdRevert to Previous Commit
We have made our changes and committed them, here’s the command to revert back to the specified commit:
git revert <previous-commit-sha1>If we want to go back to one or more previous commit without creating any revert commit, just use this command:
git reset --hard <previous-commit-sha1>Edit Previous Commit Message
git commit --amend -m "New commit message"Use Emoji with Commit Message
git commit -m ":emoji: your message."See Commit History
List the commit history
git logAlso see the changes
git log -pLimit the list to the last two items
git log -p -2Merge Specific Commit
Use this command to merge specific commit. You can use git log command to get the SHA
git cherry-pick 62ecb3Commit and Set the Date
GIT_AUTHOR_DATE="Aug 10 08:12 2018 +0200" git commit -m "message"Update Previous Push Commit Date
git commit --amend --date='Aug 10 08:12 2018 +0200' -C HEAD