Git is a distributed version control system that's widely used for tracking changes in source code during software development. Understanding basic Git commands and workflows is essential for collaborating in modern software development.
-
git init
: Initializes a new Git repository.git init
-
git clone
: Clones a repository into a new directory.git clone [url]
-
git add
: Adds files to the staging area before committing.git add [file] # Add a specific file git add . # Add all new and changed files
-
git commit
: Commits the staged changes with a descriptive message.git commit -m "Commit message"
-
git status
: Shows the status of changes as untracked, modified, or staged.git status
-
git push
: Pushes committed changes to a remote repository.git push [remote] [branch]
-
git pull
: Fetches changes from a remote repository and merges them.git pull [remote]
-
git branch
: Lists, creates, or deletes branches.git branch # List branches git branch [name] # Create a new branch git branch -d [name] # Delete a branch
-
git checkout
: Switches branches or restores working tree files.git checkout [branch] git checkout -b [new-branch] # Create and switch to new branch
-
git merge
: Merges one branch into another.git merge [branch]
-
git log
: Shows the commit logs.git log
-
Initialize a Repository (
git init
) or Clone an Existing One (git clone
):- Start a new project with
git init
or contribute to an existing project usinggit clone
.
- Start a new project with
-
Create a New Branch (
git branch
) and Switch to It (git checkout
):- Work on features or fixes in separate branches.
-
Stage Changes (
git add
):- Use
git add
to stage changes you've made in the working directory.
- Use
-
Commit Changes (
git commit
):- Commit your staged changes with a clear, descriptive message.
-
Pull Latest Changes (
git pull
):- Regularly pull the latest changes from the remote repository to stay updated.
-
Push Changes (
git push
):- Once the work is ready and committed, push your changes to the remote repository.
-
Merge Changes (
git merge
):- Merge your branch back into the main branch (like
main
ormaster
).
- Merge your branch back into the main branch (like
-
Handle Merge Conflicts:
- If there are conflicts, resolve them manually, commit the changes, and then complete the merge.
-
Delete the Feature Branch (Optional):
- Once merged, the feature branch can be deleted to keep the repository clean.
Git is a powerful tool for version control, enabling collaborative and efficient management of source code. Understanding these basic commands and the typical workflow is crucial for anyone involved in software development and coding. Git helps in tracking changes, collaborating with others, and maintaining the integrity and history of a project.