In this blog, I have listed some of the basic commands like installing and configuring the Git. How Git works is not the focus of this post. I have piled up some commands for performing basic operations in git.
1) Install Git
sudo apt-get update
sudo apt-get install git
2) Configuring username and email of git
git config --global user.email "shivangi.gupta@knoldus.in"
git config --global user.name "Shivangi Gupta"
3) Initializing git in a repository
git init
4) To see the changes in files
git status
5) Adding all changes to commit
git add .
-For all files which have been changed
git add
-Add only specified files
6) Committing staged files
git commit -m "commit message"
7) Push all added files to a repository
git push origin master
if you are pushing first time to the remote repository
git remote add origin
Now you can push your changes to the selected remote server.
8) To create a new branch
git checkout -b "branch name"
git checkout “branch name” (switch to specified branch)
9) To see all branches
git branch
-it will display all branches locally
git branch -a
– it will show local and remote branches
10) To update your local repository with remote
git pull origin "branch name"
-it will pull all changes from the remote repository
11) To rebase the local with remote branch
git pull -r origin "branch name"
12) To see all the logs and commits message
git log
13) Unfortunately, if you have added files which you don’t want to add. To undo it,
git reset
-It will unstage all staged files. Now you can add again only required files.
14) Unfortunately, you have committed unwanted files. To undo it,
git reset HEAD^
-It will revert your commit to all unstaged files.
15) If you have written a wrong commit message, you can edit it with,
git commit --amend
15) To rename a branch name
git branch -m newname
-It will rename your current branch
git branch -m oldname newname
-it will rename your specified branch name
16) To create a new branch from particular commit id
git checkout -b justin commitid
-it will create a new branch “justin” from given commit id
17) To remove files from the cache
git rm --cached -r filename
18) To delete a branch locally
git branch -D branchname
19) To delete a branch remotely
git push origin --delete branchname
20) Merging multiple commits into one commit
git rebase -i HEAD~n
-where n is number of commits you want to squash
21) Undo all changes which are not yet staged
git checkout .
22) Pointing branch to the last commit
git reset --hard HEAD
It will undo all changes which are staged as well as not staged.
23) If you want a commit from one branch to be applied to another branch, cherry-pick is there for you
- Make sure you are on the branch you want to apply the commit to.
git checkout <branch-name>
- Execute the following:
git cherry-pick <commit-hash>
Hope this compilation helps you. Share your thoughts in the comments below.
1 thought on “Git Cheat Sheet: Basics2 min read”
Comments are closed.