Skip to content

Latest commit

 

History

History
97 lines (72 loc) · 3.2 KB

Basic_Git_Commands_and_Workflow.md

File metadata and controls

97 lines (72 loc) · 3.2 KB

Version Control with Git: Basic Git Commands and Workflow

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.

Basic Git Commands

  1. git init: Initializes a new Git repository.

    git init
  2. git clone: Clones a repository into a new directory.

    git clone [url]
  3. 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
  4. git commit: Commits the staged changes with a descriptive message.

    git commit -m "Commit message"
  5. git status: Shows the status of changes as untracked, modified, or staged.

    git status
  6. git push: Pushes committed changes to a remote repository.

    git push [remote] [branch]
  7. git pull: Fetches changes from a remote repository and merges them.

    git pull [remote]
  8. 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
  9. git checkout: Switches branches or restores working tree files.

    git checkout [branch]
    git checkout -b [new-branch] # Create and switch to new branch
  10. git merge: Merges one branch into another.

    git merge [branch]
  11. git log: Shows the commit logs.

    git log

Basic Git Workflow

  1. 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 using git clone.
  2. Create a New Branch (git branch) and Switch to It (git checkout):

    • Work on features or fixes in separate branches.
  3. Stage Changes (git add):

    • Use git add to stage changes you've made in the working directory.
  4. Commit Changes (git commit):

    • Commit your staged changes with a clear, descriptive message.
  5. Pull Latest Changes (git pull):

    • Regularly pull the latest changes from the remote repository to stay updated.
  6. Push Changes (git push):

    • Once the work is ready and committed, push your changes to the remote repository.
  7. Merge Changes (git merge):

    • Merge your branch back into the main branch (like main or master).
  8. Handle Merge Conflicts:

    • If there are conflicts, resolve them manually, commit the changes, and then complete the merge.
  9. Delete the Feature Branch (Optional):

    • Once merged, the feature branch can be deleted to keep the repository clean.

Conclusion

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.