1. Configuration Commands
git config --global user.name "Your Name"
Explanation:- This will set the author name to each one of your commit messages in git repositories.
- For example: if in you local machine, you have five git repositories then it will configure user name of all five repositories.
- Means to say that in all repositories your user name will be same.
- If you remove this
--global
then user name will be limited to particular repository only. - For example let say you have two repositories named
test1
andtest2
, so you can have two user name accordingly.
git config --global user.email "your.email@example.com"
Explanation:- This will similar to user name as described above.
git config --list: Lists all your Git configurations
Explanation:- This will list all your git configurations.
2. Basic Commands
git init
Explanation:- This will Initializes a new git repository.
- Let say you are working on files which are inside a folder, switch to that folder and run the above command to Initializes a new repo so that it will be tracked via version control system.
git clone <repository-url>
Explanation:- This will clone a repository from remote.
- For example: if you have a repository on github then this command will clone everything from github repository.
git branch
Explanation:- This will list all the branches that you have on you local machine.
git branch <branch-name>
Explanation:- This will create a new branch on you local machine.
git checkout <branch-name>
Explanation:- This will switch you to the specific branch on you local machine.
git checkout -b <branch-name>
Explanation:- This will create a new branch and switch you to the newly created branch on you local machine.
git status
Explanation:- This will display the status of working directory and staged files if any.
- For example: if you are working on a repository which is in local machine and if you forgot in how many files, you made the changes then this command will display the all files where you made changes.
- And staged area is something, once you made changes in a file and you kept this file in area which is ready to be pushed to remote.
git add <file path>
Explanation:- This command is used to add specific file to the staging area.
- For example: you made changes a file and now want to make ready to be pushed, first you can check the file path via above command
git status
this will give you file path so after getting the file path you can use this command to add to the staging area. git add .
Explanation:- This command is used to all the modified or new added files to the staging area.
git reset <file>
Explanation:- This command will remove a file from staging area.
git commit -m "Commit message"
Explanation:- This command will commit the staged files with a message.
- Actually it will create a log history in local machine with that specific message so that you can remember this changes is related to what.
git commit --amend
Explanation:- This command will modify your very last commit.
- For example: you made some changes which is related to UI and you missed one file then you can add that file in staged area and use the above command to amend it the last commit.
git reset --hard <commit>
Explanation:- This command will resets the working directory and staging area to a specific commit.
- Note : you need to pass the commit id which you want to reset. And you see you commit id by
git log
git reset --soft <commit>
Explanation:- This command will move the HEAD to specific commit but keeps the changes in staging area.
- Note : you need to pass the commit id which you want to reset. And you see you commit id by
git log
git revert <commit>
Explanation:- This command will create a new commit and that will undoes the changes from previous commit.
- Note : you need to pass the commit id which you want to reset. And you see you commit id by
git log
git push
Explanation:- This command will push your commits on your current branch to remote repository.
git push origin <branch-name>
Explanation:- This command will push specific branch to the remote repository.
- For example you created new branch on your local machine and now you wanted to push this branch to remote then you can use this command.
git pull
Explanation:- This command will fetch the changes and merge to the current branch on your local machine from remote repository.
- Means to say that let say two user working on same branch so user A made some changes to pushed it to remote and if user B wants to have the changes made by user A on user B local machine then user B can use this command.
git fetch
Explanation:- This command will fetch the changes to the current branch on your local machine without merging it from remote repository.
git merge <branch-name>
Explanation:- This command will merge a branch into the current branch.
3. Working with Remotes
git remote add <name> <url>
Explanation:- This command will add a new remote repository to your working directory.
git remote -v
Explanation:- This command will display you all the remote repositores on your working directory.
git remote remove <name>
Explanation:- This command will remove a remote repository from your working directory.
git remote rename <old-name> <new-name>
Explanation:- This command will rename you remote repository.
4. Stashing
git stash
Explanation:- This command will stash your changes at your current branch in working directory.
- Let say you are working on branch A and you need to move to branch B and you do not want your changes from branch A to B then you can stash your changes in branch A and switch to branch B.
git stash apply
Explanation:- This command will take back your last stash, means to say it will apply the last stash to your current working directory.
git stash pop
Explanation:- This command will apply and remove the last stash.
git stash list
Explanation:- This command will list all the stashes.
git stash drop
Explanation:- This command will delete the specific stash.
5. Log and Diff
git log
Explanation:- This command will show all the commit history on your local machine.
git diff <file path>
Explanation:- This command will show all the changes made to particular file.
git diff
Explanation:- This command will show changes between commits, branches, and working directories.
6. Tagging
git tag <tag-name>
Explanation:- This command will create a new tag on you local machine.
git tag -a <tag-name> -m "Message"
Explanation:- This command will create an annotated tag on you local machine.
git show <tag-name>
Explanation:- This command will display the details of a specific tag on you local machine.
git push origin <tag-name>
Explanation:- This command will push a tag to the remote repository.
git push origin --tags
Explanation:- This command will push all the tags from local machine to the remote repository.
git tag -d <tag-name>
Explanation:- This command will delete the tag on your local machine.
git push origin :refs/tags/<tag-name>
Explanation:- This command will delete the tag in the remote repository.
7. Rebase and Cherry-pick
git rebase <branch-name>
Explanation:- This command will re-applies commits on the top of another base.
- Let say you working working on a branch
A
which has been created frommain
branch, and now you created new branchB
from branchA
but someone made changes to themain
branch, in this case branchB
base is different so if you want your branchB
to be in sync withmain
branch then use the above command.
git rebase --continue
Explanation:- This command will continue the rebase which was stopped due to conflicts.
- So while rebasing your branch there could be a scenario where the file you are working on and made changes can be also already changed by your peer in the
main
branch, this can be lead to conflicts and you have to resolve this conflicts before rebase.
git rebase --abort
Explanation:- This command will abort the rebase and returns to the original state.
git cherry-pick <commit>
Explanation:- This command will apply a commit from another branch to your current working branch on your local machine.
8. Git Ignore and Clean
git clean -f
Explanation:- This command will remove untracked files from the working directory for example if you created new file in working directory and you want to remove that file then you can use this command.
git clean -fd
Explanation:- This command will remove untracked files and directories from the working directory for example if you created new file/folder and you want to remove that file and folder then you can use this command.
.gitignore
Explanation:- If you want some files to be hidden from version control system then put those files here.
Ali 2 months ago
...It was helpful...