Managing a Git Repository from the Command Line

Git is a powerful tool for version control, and managing a repository through the command line is efficient for developers. This article covers the key Git commands needed to create, manage, and collaborate using a Git repository.


1. Initializing a Repository

To start versioning a project, you must first initialize a Git repository:

git init

This command creates a .git folder, which stores all versioning information.

2. Cloning an Existing Repository

To clone an existing repository from a remote server, use:

git clone <repository-url>

This command downloads the project and its history to your local machine.


3. Adding Files to Staging

To track changes in a specific file or set of files, add them to the staging area:

git add <file>

To add all modified files:

git add .

4. Committing Changes

Once files are staged, commit them to the local repository:

git commit -m "Your commit message"

Make sure to write descriptive commit messages to easily identify the changes.


5. Viewing Repository Status

Check the status of your working directory and staging area:

git status

This command shows which files have changes and whether they are staged or unstaged.


6. Pushing Changes to a Remote Repository

After committing, push the changes to a remote repository (e.g., on GitHub or GitLab):

git push origin <branch-name>

For the first push, you may need to specify the branch you’re working on.


7. Pulling Changes from a Remote Repository

To sync your local repository with the latest changes from the remote repository, pull the changes:

git pull

This updates your local files with changes from the remote repository.


8. Branching

Branches allow you to develop features independently. To create a new branch:

git branch <branch-name>

Switch to the new branch:

git checkout <branch-name>

Or both in one command:

git checkout -b <branch-name>

9. Merging Branches

When your feature is ready, merge it back into the main branch:

  1. Switch to the main branch: git checkout main
  2. Merge the feature branch: git merge <branch-name>

Resolve any conflicts that arise during the merge.


10. Deleting a Branch

After merging, you can safely delete the feature branch:

git branch -d <branch-name>

11. Checking Log History

To see the history of commits:

git log

You can also use git log --oneline to get a concise view of commits.


12. Undoing Changes

To revert unstaged changes:

git checkout -- <file>

To reset a staged file:

git reset <file>

For more advanced undoing of commits:

git revert <commit-id>

Advanced Git Commands

1. Stashing Changes

If you want to save your local changes without committing, use stash:

git stash

To retrieve stashed changes:

git stash pop

2. Rebasing Branches

To rebase your feature branch on the latest main branch:

git rebase main

This applies your changes on top of the updated main branch without a merge commit.


Conclusion

Using Git from the command line provides deep control over repository management. Whether you’re starting a new project, collaborating on a team, or experimenting with different features, mastering these Git commands is essential for efficient development workflows.

By understanding the basics—like committing, branching, and merging—you can build on this foundation and explore more advanced commands as needed.

Edvaldo Guimrães Filho Avatar

Published by

Categories:

Leave a comment