Sunday, October 27, 2024

20 Examples of Git Commands in Linux and Windows

Version control is crucial in software development, and Git stands out as a powerful tool for managing source code. Whether you're working in a Linux or Windows environment, understanding Git commands is essential. This article explores 20 fundamental Git commands, demonstrating their application and significance. From initializing a repository to handling branches and commits, each command plays a pivotal role in maintaining a well-organized and collaborative development workflow. Whether you're a seasoned developer or a newcomer, mastering these Git commands will enhance your ability to track changes, collaborate seamlessly, and contribute effectively to software projects.


20 Examples of Git command in Linux and Windows

Git is a popular distributed source control system. In last few years it has become very popular overtaking several conventional source control systems like SVN, CVS, Mercury, TFS etc. It's also one of the essential skill for software developer. 

As a Java developer I have used Git from inside IDEs like Intellij IDEA and Eclipse as well as from command prompt using tools like git bash. 

In this article, I am going to share 20 common Git examples  for software developer and programmers:


1. git init <directory>

This command create empty Git repo in specified directory. You can run this command  with no arguments to initialize the current directory as a git repository.

When you run this command, it sets up all the necessary files and data structures required for version control in the specified directory. 

Here's an example of how you can use git init with a directory:

# Open a terminal or command prompt

# Navigate to the directory where you want to initialize a Git repository
cd /path/to/your/directory

# Run the git init command to initialize a new Git repository
git init

after running these commands, you'll see a message indicating that the Git repository has been initialized. The directory will now contain a hidden subfolder named .git, which contains the internal data structure required for version control.

Remember, you typically use git init when you're starting a new project or when you want to bring an existing project under Git control. Once a repository is initialized, you can use other Git commands to manage and track changes in your project.


2) git clone <repository>

This command clones the repo located at <repo> into your local machine. Original repository can be located on the local filesystem or on a remote machine e.g. Github or BitBucket which you can access via HTTP or SSH. 

In other words, the git clone command is used to create a copy of a remote Git repository. 

Here's an example of how you can use git clone:

# Run the git clone command with the URL of the remote repository
git clone https://github.com/example/repo.git

When you run this command, Git will create a new directory with the same name as the repository (or you can specify a different directory name after the URL if you want). It will download all the files from the remote repository and set up a local Git repository that is a complete copy of the remote repository.

If the repository requires authentication, Git will prompt you to enter your credentials (username and password or a token) unless you're using SSH keys for authentication.

After the git clone command is executed, you can navigate into the newly created directory to start working with the cloned repository


3) git add <directory>

This command stage all changes in <directory> for the next commit. You can also replace <directory> with a <file> to change a specific file


4) git commit -m "<message>"

This command commits the staged snapshot, but instead of launching a text editor, it uses 

use <message> as the commit message.


git commit --amend

Replace the last commit with the staged changes and last commit combined. Use with nothing staged to edit the last commit̢۪s message.


git revert <commit>

Create new commit that undoes all of the changes made in <commit>, then apply it to the current branch.



5) git status

This command List which files are staged, unstaged, and untracked.


6) git log

This command displays the entire commit history using the default format.


7) git diff

This command shows unstaged changes between your index and working directory


8) git remote add <name> <url>

This is one of the essential git command while working with remote repositories. It creates a new connection to a remote repo. After adding a remote, you can use <name> as a shortcut for <url> in other commands.


9) git fetch <remote> <branch>

Fetches a specific <branch>, from the repo. Leave off <branch> to fetch all remote refs.


10) git pull <remote> 

Fetch the specified remote's copy of current branch and immediately

merge it into the local copy


11) git push <remote> <branch>

Push the branch to <remote>, along with necessary commits and objects. Creates named branch in the remote repo if it doesn̢۪t exist.


12) git branch

This command shows all of the branches in your repo. Add a <branch> argument to create a new branch with the name <branch>.


13) git checkout -b <branch>

Create and check out a new branch named <branch>. Drop the -b flag to checkout an existing branch.


14) git merge <branch>

This git command merges the <branch> into the current branch.


15) git clean -n 

Shows which files would be removed from working directory. Use the -f flag in place of the -n flag to execute the clean.


16) git reset 

Reset staging area to match most recent commit, but leave the

working directory unchanged.


git reset --hard Reset staging area and working directory to match most recent

commit and overwrites all changes in the working directory.


17) git rebase <base>

Rebase the current branch onto <base>. <base> can be a commit ID, a branch name, a tag, or a relative reference to HEAD.


18) git config user.name <name>

Define author name to be used for all commits in current repo. Devs commonly use --global flag to set config options for current user.


19) git log -p

Display the full diff of each commit


20) git diff HEAD 

Show difference between working directory and last commit.


git diff --cached Show difference between staged changes and last commit



That's all about some essential git commands in Linux and Windows. In order to run these commands you must install git because these commands requires git executable.


No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.