Git is a popular version control system that allows you to track changes to your codebase, collaborate with other developers, and manage your code history. Here are some important Git commands and a brief guide on how to use Git:
Getting started with Git
Install Git: Download and install Git from the official website for your platform (Windows, macOS, or Linux).
Configure Git: Once you have installed Git, you should configure your name and email address in Git by running the following commands in your terminal:
git config --global user.name "Your Name" git config --global user.email "youremail@example.com"
Install Git: Download and install Git from the official website for your platform (Windows, macOS, or Linux).
Configure Git: Once you have installed Git, you should configure your name and email address in Git by running the following commands in your terminal:
git config --global user.name "Your Name" git config --global user.email "youremail@example.com"Creating a Git repository
To start using Git with your project, you need to create a Git repository. A Git repository is a folder that contains your project files and the Git configuration files. Here's how you can create a new Git repository:
Create a new folder for your project:
mkdir my-project
cd my-project
Initialize a new Git repository:
git init
Create a new folder for your project:
mkdir my-project
cd my-project
Initialize a new Git repository:
git initGit commands
Git workflow
Create a new branch: Before you start making changes to your code, create a new branch to work on.
git checkout -b my-new-featureMake changes: Make changes to your code and add them to the staging area with
git add.git add myfile.pyCommit changes: Commit your changes with a descriptive message
git commit -m "Added new feature"Push changes: Push your changes to the remote repository.
git push origin my-new-featureCreate a pull request: Once you have pushed your changes to the remote repository, create a pull request to merge your changes into the main branch.
# on the remote repository git pull-requestReview and merge: Review the changes in the pull request and merge them into the main branch.
Git branching
Git branching is a powerful feature that allows you to work on multiple versions of your code at the same time. You can create a new branch for a specific feature or bug fix, and merge it back into the main branch once it's complete. Here are some common Git branching commands:
git branch: List all branches in the repository. git checkout -b branch-name: Create a new branch and switch to it. git checkout branch-name: Switch to an existing branch. git merge branch-name: Merge changes from the specified branch into the current branch. git branch -d branch-name: Delete a branch.To switch to a different branch, use the git checkout command:
git checkout feature-login
This will switch you to the feature-login branch.
To create a new branch we also use the git branch command:
git branch feature-login
This will create a new branch called feature-login
Git merging
Merging is the process of combining changes from one branch into another branch. There are two main types of merges in Git: fast-forward and recursive. A fast-forward merge occurs when the changes in one branch can be applied directly to the other branch without any conflicts. A recursive merge occurs when there are conflicts that need to be resolved before the changes can be merged. Here's how you can merge changes from one branch into another:Switch to the branch you want to merge into:
cssgit checkout mainMerge changes from the other branch:
sqlgit merge feature-branchResolve conflicts if necessary:
If there are conflicts between the two branches, Git will prompt you to resolve them. You can use a text editor or a merge tool to resolve the conflicts.
Commit the changes:
Once the conflicts are resolved, commit the changes to the main branch.
Viewing commit history
To view the commit history of the project, use the git log command:
bashgit log
This will show you a list of all the commits in the repository, along with their commit messages, author names, and timestamps.
Undoing changes
If you need to undo changes that have been committed to the repository, you can use the git revert command. For example, to revert the last commit, run the following command:
git revert HEAD
This will create a new commit that undoes the changes made in the previous commit. If you want to completely remove the changes from the repository, you can use the git reset command with the --hard option:
git reset --hard HEAD~1This will remove the last commit from the repository and reset the files to their state before the commit. However, be careful when using this command, as it will remove all changes made in the last commit and cannot be undone.
Checking the status of the repository
To check the status of the repository and see which files have been modified or added to the staging area, use the git status command:
git status
This will show you a list of all the modified files and their current status.
Pushing changes to a remote repository
To push your changes to a remote repository, first add the remote repository URL using the git remote command:
git remote add origin git@github.com:username/project.git
This will add a new remote called origin with the specified URL. Then, to push your changes to the main branch of the remote repository, use the git push command:
git push origin main
Pulling changes from a remote repository
To pull changes from a remote repository, use the git pull command:
git pull origin main
This will fetch the changes from the main branch of the remote repository and merge them into your current branch.
Cloning a remote repository
To clone a remote repository to your local machine, use the git clone command:
git clone git@github.com:username/project.git
This will create a new directory called project on your local machine and clone the remote repository into it.
Comments