These are common Git commands used in various situations:
Git conceptually revolves around a repository. To begin working we need a repository on our local machine. We can either create a new repository or copy an existing repository.
To create a new git repository directory, (or to convert an existing source directory to a git repository), go to your desired directory in a terminal and provide the command
This would create a
.git directory inside the repository directory. If this directory is already a git repository, it would be re-initialized as a new repository.
git clone <repository_path> [directory]
To copy a repository from remote location to a new directory, we can make use of
git clone command. This comes handy, when we want to contribute to an already existing code, on which other friends have already started working. Since this command connects to remote repository, it may require internet connection if the remote repository lies outside local network. Running it will create a directory as per the remote repo name.
If you want to clone repository in a custom directory name, you may pass that as an argument
git add <file.path>
Git maintains an index of all the files to be committed. If we are working on multiple files and want to add files selectively to the index, this command helps us in doing so.
A non-indexed file would show in red (default colour) and an indexed file would show in green when status is checked.
git mv <file.path> <new-file.path>
mv helps renaming or moving an existing file to new path.
git reset <file.path>
It Resets current HEAD to the specified state. It is reverses the git add command.
rm Remove files from the working tree and from the index
Bisect is a group of commands provided by git which use binary search to find a particular commit. E.g. if a commit caused an issue, but there are many commits after that and we want to find that particular commit, we can make use of this tool.
The first command is
git bisect start . This starts the tool.
Now, we have to let this tool know about a commit which we are sure which is good.
git bisect good <commithashcode>
Now, we have to let this tool know about a commit which we are sure which is bad.
git bisect bad <commithashcode>
Now, git takes us to a commit, which we needs to check if this commit is good or bad and inform git by using command
git bisect good/bad
This goes on, until we reach the commit that we are looking for.
To go back to original commit (from where we started bisecting) use the command
git bisect reset.
This command looks for specified pattern in the working tree and prints the matching lines.
This command shows the commit logs from history
It shows various types of objects depending upon the input flags
It shows the working tree status
It Lists, creates, or deletes specified branches
checkout Switch branches or restore working tree files
commit Record changes to the repository
diff Show changes between commits, commit and working tree, etc
merge Join two or more development histories together
rebase Forward-port local commits to the updated upstream head
tag Create, list, delete or verify a tag object signed with GPG
fetch Download objects and refs from another repository
pull Fetch from and integrate with another repository or a local branch
push Update remote refs along with associated objects