1.

Git Commands

Answer»

The following table shows the most commonly used Git Commands:

S. NoCommand Name Use
1git initInitialise a local Git Repository
2git add.Add one or more files to the staging area
3git commit -m “Commit Message”Commit changes to the head but not to the remote repository.
4git statusCheck the status of your current repository and list the files you have changed.
5git logProvides a list of all commits made on a branch
6git diffView the changes you have made to the file
7git push origin <branch name>Push the branch to the remote repository so that others can use it.
8git config --global user.name “Name”Tell Git who you are by configuring the author name
9git config --global user.email user@email.com  Tell Git who you are by configuring the author email id.
10git clone <repository_name>Creates a Git repository copy from a remote source
11git remote add origin <server>Connect your local repository to the remote server and add the server to be able to push it.
12git branch <branch_name>  Create a new branch
13git checkout <branch_name>Switch from one branch to another
14git merge <branch_name>Merge the branch into the active branch
15git rebaseReapply commits on top of another base tip
16git checkout -b <branch_name>Creates a new branch and switch to it
17git stashStash changes into a dirty working directory
18git pullUpdate local repository to the newest commit
19git revert <commit_id>Revert commit changes
20git clean -nShows which files would be removed from working directory. Use the -f flag in place of the -n flag to execute the clean.
21git log --summaryView changes (detailed)
22git diff HEADShow difference between working directory and last commit.
23git log --onelineView changes (briefly)
24git reflogShow a log of changes to the local repository’s HEAD. Add --relative-date flag to show date info or --all to show all refs.
25git rebase -i <base>Interactively rebase current branch onto <base>. Launches editor to enter commands for how each commit will be transferred to the new base.
26git restore --staged <file_name>Resetting a staged file
27git rm -r [File_name]Remove a file (or folder)
28git config --listList all variables set in config file, along with their values
29git branch -d <local_branch>  Delete local branch in Git
30git push -d <remote_name> <branch_name>Delete remote branch in Git
31git stash popUnstash the changes
32git commit -amThe -am along with the command is to write the commit message on the command line for already staged files.
33git commit -ammendThe amend is used to edit the last commit. Incase we need to change the last committed message, this command can be used.
34git rmThe git rm command is used to remove or delete files from working tree and index.
35git pull --rebaseGit rebase is used to rewrite commits from one branch to another branch.
36git merge --squashThe squash along with git merge produces the working tree. It indexes in the same way as that of the real merge, but discards the merge history.
37git revert -e <commit_id>edit the commit mesage before reverting, -e is used for the same.
38git bisectGit bisect goes through all the previous commit and uses binary search to find the bugged commit.
39git blamegit blame is used to know who/which commit is responsible for the lastest changes in the repository.
40git cherry-pickChoosing a commit from one branch and applying it to another is known as cherry picking in Git.
Conclusion

So, we learnt how Git makes managing large software codebases easier, how you can commit changes, clone the repository, how branches work and many git commands that reduce the burden from the lives of developers.

To test your understanding of Git, some MCQ are provided. Pick the correct option.

 

Important Resources


  • Git Vs Github

  • Git Commands

  • Git Interview Questions




Discussion

No Comment Found