Top Command of Git

·

3 min read

1. git init

Usage: git init [repository name]

We have to navigate to our project directory and type the command git init to initialize a Git repository for our local project folder. Git will create a hidden .git directory and use it for keeping its files organized in other subdirectories.

2. git add

Usage (i): git add [file(s) name]

This will add the specified file(s) into the Git repository, the staging area, where they are already being tracked by Git and now ready to be committed.

Usage (ii): git add . or git add *

This will take all our files into the Git repository, i.e., into the staging area.

We can use this command as git add -A as well.

Note: We will have to commit our files after we add them to the staging area.

3. git commit

Usage: git commit -m “message”

This command records or snapshots files permanently in the version history. All the files, which are there in the directory right now, are being saved in the Git file system.

4. git status

Usage: git status

This command will show the modified status of an existing file and the file addition status of a new file, if any, that has to be committed.

5. git remote

Usage: git remote add origin “[URL]”

Once everything is ready on our local system, we can start pushing our code to the remote (central) repository of the project.

6. git push

Usage: git push origin [branch name]

Suppose, we have made some changes in the file and want to push the changes to our remote repository on a particular branch. By using the command ‘git push,’ the local repository’s files can be synced with the remote repository on Github.

7. git clone

Usage: git clone [URL]

Suppose, we want to work on a file that is on a remote Github repository as another developer. How can we do that? We can work on this file by clicking on Clone or Download and copying the link and pasting it on the terminal with the git clone command.

Note: Here, we don’t have to use the git remote add origin command because we have already cloned the remote repository in the local directory. Now, if we push any new file, it knows where it has to go.

8. git branch

Usage (i): git branch [name-of-the-branch]

When multiple developers are collaborating on a project or repository, branches become essential for managing different workspaces. Using this command, we can create a new branch (for example, ‘branch1’). This allows developers to work independently on their respective branches, making changes and commits without affecting the main branch or other branches.

Usage (ii): git branch -D [name-of-the-branch]

Likewise, to delete a branch, we utilize the “git branch -D” command. This enables us to remove a specific branch (e.g., ‘name-of-the-branch’) that is no longer needed, cleaning up the repository and reducing clutter.

9. git checkout

Usage (i): git checkout [name-of-the-new-branch]

This command allows us to switch to an existing branch within our repository. It facilitates navigating to the desired branch, enabling us to add new files, make changes, and commit those files within that specific branch.

10. git log

Usage (i): git log

The “git log” command is handy when we want to examine the detailed log of every commit in our repository. By executing this command, we can view the log specific to the branch we are currently in. Additionally, we can use “git log -3” to display the last three logs.

Usage (ii): git log –graph

For a visual representation of the commit history, we can utilize “git log –graph”. This option presents the commit-graph, showcasing the branching and merging of commits

Usage (iii): git log –graph –pretty=oneline

To further customize the output, we can use “git log –graph –pretty=oneline”. This format displays the commit graph along with a concise one-line description for each commit.