You are currently viewing A Beginner’s Guide to Git Fundamental Commands

A Beginner’s Guide to Git Fundamental Commands

  • Post author:
  • Post category:Git
  • Post comments:0 Comments
  • Post last modified:January 24, 2024

Git is a distributed version control system widely used for tracking changes in source code during software development. Here is a tutorial covering some basic Git commands along with examples:

1. Initialize a Git Repository:

To start version controlling a project, you need to initialize a Git repository.

git init

2. Clone a Repository:

To get a copy of an existing Git repository, you can use the git clone command.

git clone <repository_url>

3. Check Repository Status:

To see the status of changes as untracked, modified, or staged:

git status

4. Stage Changes:

Before committing changes, you need to stage them using git add.

git add <file_name>

To add all changes:

git add .

5. Commit Changes:

After staging changes, commit them with a meaningful message.

git commit -m "Your commit message here"

6. View Commit History:

To see the commit history:

git log

7. Create a New Branch:

To create a new branch for new features or bug fixes:

git branch <branch_name>

8. Switch Between Branches:

To switch to a different branch:

git checkout <branch_name>

Or use the combined command:

git checkout -b <new_branch_name>

9. Merge Branches:

To merge changes from one branch into another:

git merge <branch_name>

10. Remote Repositories:

To manage remote repositories:

  • Add a remote repository:
  git remote add origin <remote_repository_url>
  • Push changes to a remote repository:
  git push -u origin master
  • Pull changes from a remote repository:
  git pull origin master

11. Undo Changes:

To discard changes in your working directory:

git checkout -- <file_name>

To undo the last commit (careful with this in a shared environment):

git reset HEAD^

12. Tagging:

To tag specific commits for software releases:

git tag -a v1.0 -m "Release version 1.0"

13. Git Ignore:

Create a .gitignore file to specify untracked files that Git should ignore.

Example .gitignore file:

# Ignore compiled files
*.class

# Ignore log files
*.log

# Ignore build output
/target/
/build/

14. Stash Changes:

To temporarily save changes without committing:

git stash

Later, you can apply the changes back:

git stash apply

15. Fetch Updates:

To get the latest changes from a remote repository without merging:

git fetch

Conclusion:

These are some fundamental Git commands to get you started. Git has a rich set of features, and as you become more comfortable with these basics, you can explore more advanced commands and workflows.

Leave a Reply