In our day to day software development version control helps us to protect from accidental data loss. Git is a free open source version control system. Compare to other repository Git is rich with Frictionless Context Switching, Role-Based Code lines, Feature Based Workflow & Disposable Experimentation. To read more about these features please visit http://git-scm.com. While working in Git version control there are many Frequently used Git Commands to operate Git. In this session let me share Git Commands checklist for Developers.
Git Commands
Git is a powerful version control system widely used by developers to manage changes to source code over time. Understanding fundamental Git commands is essential for collaborating on projects and maintaining a clean, organized codebase. This article explores various Git commands, their functions, and best practices for using them effectively.
Commands to Create & Clone
To create a new repository – git init
To clone local repository – git clone /path/to/repository
To clone remote repository – git clone username@host:/path/to/repository
Git Commands to Add & Remove
Add changes to Index – git add <filename>
Add all changes to Index – git add *
Remove/Delete – git rm <filename>
Git Commands to Commit & Synchronize
To commit changes – git commit -m “Commit message.”
Push changes to remote repository – git push origin master
Connect local repository to remote repository – git remote add origin <server>
Update local repository with remote changes – git pull
Git Commands for Branches
Create a new branch – git checkout -b <branch>
Switch to master branch – git checkout master
To delete a branch – git branch -d <branch>
Push branch to remote repository – git push origin <branch>
Commands for Merge
Merge changes from another branch – git merge <branch>
View changes between two branches – git diff <source_branch> <target_branch>
Commands for Tagging
Create tag – git tag <tag> <commit ID>
Get Commit Ids – git log
Commands to Restore
Replace working copy with latest from Head – git checkout — <filename>
Setting Up Git
Before diving into specific commands, it is crucial to ensure that Git is installed and configured on your system. You can download Git from the official website and follow the installation instructions for your operating system.
## Configuring Git
Once Git is installed, you should configure your identity, which is essential for tracking contributions. Use the following commands:
“`bash git config –global user.name “Your Name” git config –global user.email “your.email@example.com” “`
These commands set your name and email address, which will be associated with your Git commits.
Creating a Repository
A Git repository is where your project files and the history of changes are stored. You can create a new repository in two ways: initializing a new repository or cloning an existing one.
## Initializing a New Repository
To create a new Git repository, navigate to your project directory and run:
“`bash git init “`
This command initializes a new Git repository, creating a `.git` directory that contains all necessary files for version control.
## Cloning an Existing Repository
To clone an existing repository, use the following command:
“`bash git clone “`
This command creates a local copy of the specified repository, including its entire history.
Basic Git Commands
Once you have a repository set up, you can begin to use various Git commands to manage your files.
## Checking Repository Status
To view the current status of your repository, including staged, unstaged, and untracked files, use:
“`bash git status “`
This command provides insight into the changes made to the repository since the last commit.
## Adding Changes
Before committing changes, you must stage them. Use the following command to stage changes:
“`bash git add “`
To stage all changes in the repository, use:
“`bash git add . “`
Staging files prepares them for the next commit.
## Committing Changes
Committing is the process of saving your staged changes to the repository’s history. Use the following command:
“`bash git commit -m “Your commit message” “`
The `-m` option allows you to include a brief message describing the changes made.
## Viewing Commit History
To view the history of commits in your repository, use:
“`bash git log “`
This command displays a list of commits, including commit IDs, authors, dates, and messages. You can use various options to customize the output, such as `–oneline` for a condensed view.
Remote Repositories
Working with remote repositories allows multiple people to collaborate on a project. Git provides several commands to manage remote repositories.
### Adding a Remote Repository
To add a remote repository, use:
“`bash git remote add “`
The default remote is usually named `origin`.
### Fetching Changes
To fetch changes from a remote repository without merging them, use:
“`bash git fetch “`
This command updates your local repository with the latest changes from the remote.
### Pulling Changes
To fetch and merge changes in one command, use:
“`bash git pull “`
This command is useful for updating your local branch with the latest commits from the remote repository.
### Pushing Changes
To upload your local commits to a remote repository, use:
“`bash git push “`
This command sends your changes to the specified branch on the remote repository.
## Tagging
Tags are used to mark specific points in your project’s history, often used for releases. You can create and manage tags using the following commands.
### Creating a Tag
To create a lightweight tag, use:
“`bash git tag “`
For an annotated tag that includes a message, use:
“`bash git tag -a -m “Tag message” “`
### Listing Tags
To view all tags in your repository, use:
“`bash git tag “`
### Pushing Tags to Remote
To push your tags to a remote repository, use:
“`bash git push “`
To push all tags, use:
“`bash git push –tags “`
Discarding Changes
To discard changes in a file that have not been staged, use:
“`bash git checkout — “`
If you want to unstage a file that has been added, use:
“`bash git reset “`
### Reverting Commits
To revert changes made in a specific commit, use:
“`bash git revert “`
This command creates a new commit that undoes the changes made in the specified commit.
Best Practices
When using Git, adhering to best practices can enhance collaboration and maintain a clean project history.
1. Commit Often: Make smaller, more frequent commits rather than large ones. This helps in tracking changes more effectively.
2. Write Clear Commit Messages: Use descriptive messages that explain the purpose of the changes. This aids in understanding the project history.
3. Branch for Features: Create branches for new features or bug fixes to keep the main branch stable.
4. Merge Regularly: Frequently merge changes from the main branch into your feature branches to minimize conflicts.
5. Use Tags for Releases: Tag releases to easily identify stable versions of your project.
Conclusion
Mastering Git commands is crucial for effective version control and collaboration in software development. By understanding the core commands and best practices outlined in this article, you can streamline your workflow, enhance productivity, and maintain a well-organized codebase. As you continue to work with Git, these foundational commands will become second nature, enabling you to manage your projects with confidence and efficiency.