Tuesday, March 8, 2022

50 Git Commands You Should Know

 Hello everyone, today I will show 50 git command which is really useful in developing the project. Before moving forward let’s know what is git.

What is GIT ?

Git is software for tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development. Its goals include speed, data integrity, and support for distributed, non-linear workflows.

1. Check your Git configuration

The below command returns a list of information about your git configuration including user name and email:

HTML

2. How to setup your Git username:

By using the below command you can setup your git username

HTML

3. How to setup your Git user email:

To setup the user email address you’ll use in your commits.

HTML

4. How to cache your login credentials in Git:

You can store login credentials in the cache so you don’t have to type them in each time. Just use the below command:

HTML

5. To initialize a Git repo:

The first step is to initialize a new Git repo locally in your project root. You can do so with the command below:

HTML

6. How to add a file to the staging area in Git:

The command below will add a file to the staging area. Just replace filename_here with the name of the file, you want to add to the staging area.

HTML

7. How to add all file at once

to add all files in your project to the staging area you can use .

HTML

8. How to add only certain files to the staging area in Git

With the asterisk in the command below, you can add all files starting with ‘fil’ in the staging area.

HTML

9. How to check a repository’s status in Git:

Below command will show the status of the file

HTML

10. How to commit changes in the editor in Git:

This command will open a text editor in the terminal where you can write a full commit message.

HTML

11. How to commit changes with a message in Git:

This command lets you only specify a short summary for your commit message.

HTML

12. How to see your commit history in Git:

This command shows the commit history for the current repository:

HTML

13. How to commit changes (and skip the staging area) in Git:

You can add and commit tracked files with a single command by using the -a and -m options.

HTML

14. How to see your commit history including changes in Git:

This command shows the commit’s history including all files and their changes:

HTML

15. How to see a specific commit in Git:

This command shows a specific commit.

HTML

16. How to see log stats in Git:

This command will cause the Git log to show some statistics about the changes in each commit, including line(s) changed and file names.

HTML

17. How to see changes made before committing them using “diff” in Git:

You can pass a file as a parameter to only see changes on a specific file.
git diff shows only unstaged changes by default.

We can call diff with the --staged flag to see any staged changes.

HTML

18. How to see changes using “git add -p”:

This command opens a prompt and asks if you want to stage changes or not, and includes other options.

HTML

19. How to remove tracked files from the current working tree in Git:

This command expects a commit message to explain why the file was deleted.

HTML

20. How to rename files in Git:

This command stages the changes, then it expects a commit message.

HTML

21. How to ignore files in Git:

Create a .gitignore file and commit it.

22. How to revert unstaged changes in Git:

HTML

23. How to revert staged changes in Git:

You can use the -p option flag to specify the changes you want to reset.

HTML

24. How to amend the most recent commit in Git:

git commit --amend allows you to modify and add changes to the most recent commit.

HTML

!!Note!!: fixing up a local commit with amend is great and you can push it to a shared repository after you’ve fixed it. But you should avoid amending commits that have already been made public.

25. How to rollback the last commit in Git:

git revert will create a new commit that is the opposite of everything in the given commit.
We can revert the latest commit by using the head alias like this:

HTML

26. How to rollback an old commit in Git:

You can revert an old commit using its commit id. This opens the editor so you can add a commit message.

HTML

27. How to create a new branch in Git:

By default, you have one branch, the main branch. With this command, you can create a new branch. Git won’t switch to it automatically – you will need to do it manually with the next command.

HTML

28. How to switch to a newly created branch in Git:

When you want to use a different or a newly created branch you can use this command:

HTML

29. How to list branches in Git:

You can view all created branches using the git branch command. It will show a list of all branches and mark the current branch with an asterisk and highlight it in green.

HTML

30. How to create a branch in Git and switch to it immediately:

In a single command, you can create and switch to a new branch right away.

HTML

31. How to delete a branch in Git:

When you are done working with a branch and have merged it, you can delete it using the command below:

HTML

32. How to merge two branches in Git:

To merge the history of the branch you are currently in with the branch_name, you will need to use the command below:

HTML

33. How to show the commit log as a graph in Git:

We can use --graph to get the commit log to show as a graph. Also,
--oneline will limit commit messages to a single line.

HTML

34. How to show the commit log as a graph of all branches in Git:

Does the same as the command above, but for all branches.

HTML

35. How to abort a conflicting merge in Git:

If you want to throw a merge away and start over, you can run the following command:

HTML

36. How to add a remote repository in Git

This command adds a remote repository to your local repository (just replace https://repo_here with your remote repo URL).

HTML

37. How to see remote URLs in Git:

You can see all remote repositories for your local repository with this command:

HTML

38. How to get more info about a remote repo in Git:

Just replace origin with the name of the remote obtained by
running the git remote -v command.

HTML

39. How to push changes to a remote repo in Git:

When all your work is ready to be saved on a remote repository, you can push all changes using the command below:

HTML

40. How to pull changes from a remote repo in Git:

If other team members are working on your repository, you can retrieve the latest changes made to the remote repository with the command below:

HTML

41. How to check remote branches that Git is tracking:

This command shows the name of all remote branches that Git is tracking for the current repository:

HTML

42. How to fetch remote repo changes in Git:

This command will download the changes from a remote repo but will not perform a merge on your local branch (as git pull does that instead).

HTML

43. How to check the current commits log of a remote repo in Git

Commit after commit, Git builds up a log. You can find out the remote repository log by using this command:

HTML

44. How to merge a remote repo with your local repo in Git:

If the remote repository has changes you want to merge with your local, then this command will do that for you:

HTML

45. How to get the contents of remote branches in Git without automatically merging:

This lets you update the remote without merging any content into the
local branches. You can call git merge or git checkout to do the merge.

HTML

46. How to push a new branch to a remote repo in Git:

If you want to push a branch to a remote repository you can use the command below. Just remember to add -u to create the branch upstream:

HTML

47. How to remove a remote branch in Git:

If you no longer need a remote branch you can remove it using the command below:

HTML

48. How to use Git rebase:

You can transfer completed work from one branch to another using git rebase.

HTML

Git Rebase can get really messy if you don’t do it properly. Before using this command I suggest that you re-read the official documentation here

49. How to run rebase interactively in Git:

You can run git rebase interactively using the -i flag.
It will open the editor and present a set of commands you can use.

HTML

50. How to force a push request in Git:

This command will force a push request. This is usually fine for pull request branches because nobody else should have cloned them.
But this isn’t something that you want to do with public repos.

HTML

No comments:

Post a Comment