Basic GIT Commands

In your journey through the world of version control, mastering basic GIT commands is crucial. These commands serve as the building blocks for versioning your projects, collaborating with others, and maintaining an efficient workflow. Let’s dive into some essential GIT commands you need to know, complete with examples to illustrate their use.

1. git init

Before you can effectively use GIT, you first need to initialize your repository. The git init command turns a directory into a GIT repository, allowing you to start tracking changes.

mkdir my-project
cd my-project
git init

This creates a new directory named my-project and initializes an empty repository in it. You’ll see a new .git folder created—this is where GIT stores all your project’s revision history.

2. git add

Once you've made changes to your files, you'll need to stage them before committing. The git add command allows you to add changes to the staging area.

git add filename.txt

To stage multiple files, you can either list them out:

git add file1.txt file2.txt

Or use a wildcard to add all modified files:

git add .

3. git commit

After staging your changes, it’s time to save them in the repository. The git commit command captures a snapshot of your changes. It’s important to include a clear and concise message explaining what changes were made.

git commit -m "Add initial project files"

Always write meaningful commit messages! This practice helps everyone understand the history of the project better.

4. git status

At any moment, you can check the current state of your repository to see which files have been staged, committed, or modified. The git status command is simple but powerful.

git status

This will output a summary of your working directory and staging area, helping you understand your project's state.

5. git log

To view the complete history of your commits, you can use the git log command. This shows a chronological list of commits made to the repository along with their identifiers.

git log

If you want a more concise view, you can use:

git log --oneline

This will present each commit on a single line, making it easier to scan through the history.

6. git branch

Branches are critical to GIT workflow, allowing multiple versions of your project to exist simultaneously. To create a new branch, use the git branch command followed by the branch name.

git branch new-feature

You can list all branches in your repository with:

git branch

The current branch will be highlighted with asterisks.

7. git checkout

Switching between branches can be done using the git checkout command. This command loads the specific branch you want to work on.

git checkout new-feature

If you want to create and switch to a new branch simultaneously, combine the -b option:

git checkout -b another-feature

8. git merge

Once your feature branches are ready to be integrated back into the main branch (usually main or master), you’ll want to use the git merge command. First, switch to the branch you wish to merge into:

git checkout main

Then execute the merge command:

git merge new-feature

This will combine changes from new-feature into main.

9. git remote

In collaborative projects, you’ll often interact with remote repositories. The git remote command allows you to manage these remote repositories. To add a new remote, use:

git remote add origin https://github.com/username/repo.git

You can list the remotes you have set up with:

git remote -v

10. git push

The git push command is essential for sending your committed changes from your local repository to a remote repository. It’s often used after merging branches.

git push origin main

Make sure to replace main with the name of the branch you want to push. This keeps other collaborators updated with your latest changes.

11. git pull

To fetch and integrate changes made by others in the remote repository into your local branch, you’ll use the git pull command. This combines a git fetch and git merge into one step.

git pull origin main

This command ensures that your local repository is up-to-date with the remote repository.

12. git clone

When working with an existing remote repository, instead of initializing a new one, you can clone it. The git clone command creates a copy of the remote repository on your local machine.

git clone https://github.com/username/repo.git

This command will download the entire history and set up the remote tracking as well.

13. git rm

When files need to be removed from your project, you can use the git rm command. This command will delete the file from both your working directory and the staging area.

git rm filename.txt

After removal, remember to commit your changes:

git commit -m "Remove obsolete file"

14. git reset

If you need to undo changes that have been staged (but not yet committed), the git reset command will unstage those files.

git reset filename.txt

You can also reset all staged changes with:

git reset

Conclusion

Mastering these basic GIT commands will significantly enhance your version control skills and allow you to work efficiently within your team. As you get accustomed to these commands, remember that with GIT, practice is key. Get hands-on with your projects, and don't hesitate to dig deeper into more advanced commands and features as you grow. Happy coding!