Do you find it hard to follow 1.5 hours-long git tutorials? Or do you get a headache when you open git documentation? This blog is for you!
While learning git, I made notes of all the useful commands that a beginner needs to know. This way you won't be wasting too much of your time on watching boring tutorials.
I will provide a PDF of this article at the end so please share it with your friends and you can bookmark this as well for future reference!
Editing Configuration
- git config --global user.name "your_username"
to set the username of the user committing code
- git config --global user.email "your_email"
to set the email of the user
- git config -- global core.editor "code --wait"
Asks the system to open the global info stored in the default code editor and the terminal waits till the file is closed
These are the command to set your username and there are three levels of setting details:
- system -> for all users on your PC
- global -> for all repository and current user
- local -> for this repository .
Basic Commands
- git init
to initialize the git repository at the current directory
- git status
to display the files that are untracked in red color or in the staging area and show the changes in the files
git add .
to add all the files to the staging area
git add *.js
to add only those files with the same extension
- git add file1.txt
to add just a specific file
- git commit -m "short message"
to add snapshots to your repository with a short message
git commit
just running this command will open the code editor to write a complete description if >you don't write a short message
git commit --am "fix the bug"
skipping the staging area and committing the file directory into the local repository
- git ls-files
to display files in the staging area
- git mv file1.txt main.js
command that renames the file and adds the changes to the staging area
- echo file1.txt > .gitignore
to add files to gitignore and all these files that are in gitignore will not get added to the repository
- git rm --cached file1.txt
removes the file from the index only
- git status -s
to see the short status of the files
- git restore --staged file_name
restores the exact previous state of the file and either removes it from the index and make it untracked if you have just staged that file or make it an unstaged file if you have committed that file
git show commit_key
to show the changes in a specific commit
Tip: Just write 4-5 characters of the commit key that should be enough
- git show HEAD~2
to show the changes in the previous commit (according to the example HEAD is the current commit to show the status of the commit that was exactly 2 commits before)
Remove a File from Staging Area
Step1. rm file2.txt
deletes the file you want to remove but the file remains intact in your directory
Step2. git add file2.txt
this add command will remove the file from the staging area
Step 3. git rm file1.txt
this command removes the file from the staging area as well as from the directory.
Commit Logs
- git log
to display all the commits starting from the beginning to the end
- git log --online
to display just the commit key and the message along with it
- git log --reverse
to display commits starting from the latest to the old commits
Tip: Press space to go to the next page
- git log --oneline --patch
to display changes made and with other details like commit author and his email etc
- git log --oneline --author = "author_name"
display all the commits made by a particular author
- git log --oneline --before = "YYYY-MM-DD"
display all the commits made before a specific date
- git log --oneline --after = "YYYY-MM-DD" --before = "YYYY-MM-DD"
display all the commits made after 15 march and before 28 May
- git checkout commit_key
to go to previous versions of the file
Tip: To comeback to the latest commit run the command: git checkout master
Restoring a Deleted File
Step 1. Look for the commit in which that file existed
Step 2. git checkout commit_key deleted_file_name
run this command with the full name of the file and the commit key in which the file existed
Step 3. git commit -m "msg"
run this command to do a new commit along with the deleted file
Tagging Commits
- git tag tag_name commit_key
to label commits with some names like v1.0 to denote the first version of the program
- git tag
to show the list of all the commits with labels
- git tag -n
to show the list of commits with messages
- git tag -a tag_name -m "your_message"
to label commits and it will store more details like the name of the person labeling, date and time, etc
Branch
- git branch branch_name
to create a new branch
- git switch branch_name
to switch/change to a particular branch
- git branch -m old_name new_name
to rename the git branch
- git branch -d branch_name
deletes the branch only if the branch has been pushed and merged
- git branch -D branch_name
deletes the branch forcefully
- git log master .. branch_name
show the list of commits that a particular branch is ahead of the master branch
Git doesn't let you change the branch if you have not committed the changes otherwise your changes will get removed!
So if you have to change the branch without committing the code, you can use stash to store your changes.
Stash
- git stash push -m "your_message"
to put all tracked files change in the stash but changes in untracked files can get removed!
- git stash push -am "your_message"
to put all files change in the stash
- git stash list
to display the list of stash
- git stash apply stash_key
to get back all the files and changes that were not even committed
Merging Branches
Merges are of two types:
let's say we have two branches a master branch and a featured branch
fast forward: if the featured branch is simply ahead of the master branch and there have been no changes in the master branch since the featured branch was created or since the master branch was merged with the featured branch last time and their path has not yet diverged
3 way: if the master and the featured branch both have been updated and their paths have diverged.
- git log --oneline --all --graph
to see commits across all the branches
- git merge branch_name
to merge two branches
- git branch --merged
to show the list of branches that are merged
- git branch --no-merged
to show the list of branches that are not merged
- git merge --abort
run this command to abort the merge if you can't solve the problem or decided not to merge branches
Resolving merge conflicts
Step 1. git status
to see merge conflicts that have occurred and which file(s) has error merging
Step 2. code file_name
open that file in the code editor to solve the conflict manually and close the file to go to terminal
Step 3. git add file_name
add the file to the staging area
Step 4. git commit
commit the changes in the file and the code editor will open to write out the complete message about the merge
Remotes
- git remote set-url remote_name "url"
to change the name of the remote
- git remote
to show the list of all the remotes created
- git remote -v
to show the fetch and push URL of remote in the current repository
- git remote add short_name remote_url
to add a new remote to the repository
- git remote rm remote_name
to remove a remote by its name
- git remote rename old_name new_name>
to rename a remote
Push to Repository
- git push remote_name branch_name
to push files from the local repository to the remote repository
These are the essential commands that new developers should know and this blog is kind of reference. You can remember these commands but there is no read of it as you can always come and look for specific commands.
Here is the PDF of the cheatsheet: Google Drive
Be the first among your friends to share this article on Twitter ! ❤️