Common Commands
1. Initialization and Configuration
-
git init
: Initialize a new Git repository. -
git config --global user.name "Your Name"
: Set your Git username. -
git config --global user.email "your.email@example.com"
: Set your Git email.
2. Basic Commands
-
git clone <repository_url>
: Clone a repository from a remote server. -
git status
: Check the status of your working directory. -
git add <file>
: Stage changes for the next commit. -
git commit -m "Commit message"
: Commit staged changes. -
git push
: Push committed changes to a remote repository. -
git pull
: Fetch and merge changes from the remote repository. -
git fetch
: Download changes from the remote repository without merging them. This allows you to review before merging.
3. Branching and Merging
-
git branch
: List all branches. -
git branch <branch_name>
: Create a new branch. -
git checkout <branch_name>
: Switch to a different branch. -
git merge <branch_name>
: Merge a branch into the current branch.
4. History and Diff
-
git log
: Show commit history. -
git diff
: Show changes between commits, branches, etc.
5. Undoing Changes
-
git checkout -- <file>
: Discard changes in a working directory. -
git reset <commit>
: Undo commits while preserving changes in the working directory. -
git stash
: Temporarily save (stash) changes in a working directory without committing, so you can switch branches or pull updates safely. Usegit stash pop
to apply stashed changes later.
Full Workflow
Starting a project from scratch and implementing a new feature
1. Set Up a New Git Repository
- Initialize the Git Repository: Create a new project folder and initialize a Git repository.
mkdir my-new-project
cd my-new-project
git init
- Set Global Git Configuration (if not done previously):
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
2. Set Up a Remote Repository
Create a remote repository (e.g., on GitHub, GitLab, Bitbucket) and link your local repo to it.
Connect to Remote
git remote add origin https://github.com/your-username/your-repository.git
3. Create an Initial Commit
- Add basic project setup files like
README.md
and.gitignore
touch README.md
echo "node_modules/" >> .gitignore
git add .
git commit -m "Initial project setup with README and .gitignore"
git push -u origin main
4. Create Initial Branches
- Create a
develop
branch (recommended) for ongoing development, keepingmain
for production-ready code.
git checkout -b develop
git push -u origin develop
5. Set Up Project Dependencies
- Initialize your project with any required dependencies. For example, for Node.js:
npm init -y
npm install express # Example dependency
git add .
git commit -m "Initialize project with basic dependencies"
git push
6. Create a Feature Branch
- Create a new branch for your feature to keep the
develop
branch clean.
git checkout -b feature/new-feature
7. Work on the Feature
- Implement the feature by writing the necessary code. Make sure to commit frequently with clear, descriptive commit messages.
git add .
git commit -m "Implement login functionality"
8. Pull Latest Changes from the Target Branch
-
Before pushing, ensure your feature branch is up-to-date with the latest code from
develop
(ormain
):- Switch to the Target Branch:
git checkout develop
- Pull Latest Changes:
git pull origin develop
- Switch Back to Your Feature Branch:
git checkout feature/new-feature
- Merge the Latest Changes:
git merge develop
- Resolve Merge Conflicts (if any): If conflicts arise, Git will flag them, and youβll need to manually resolve conflicts in the affected files. Once resolved:
git add <file> git commit -m "Resolve merge conflicts"
9. Test Your Feature
- Ensure the feature works by testing locally:
- Run Unit Tests.
- Manual Testing: Run the application and verify that the feature works as expected.
10. Push the Feature Branch to the Remote Repository
- After testing and merging the latest changes from
develop
, push your feature branch to the remote repository.
git push origin feature/new-feature
11. Create a Pull Request (PR)
- On your Git hosting platform (GitHub, GitLab, etc.), create a Pull Request (PR) to merge your feature branch into
develop
(ormain
).- Provide a Description: Explain what the feature does and highlight any important points for the reviewer.
- Link to Issues (if applicable): If the feature resolves any issues, link to them in the PR.
12. Code Review and Address Feedback
Collaborate with other developers for code review.
Make Changes Based on Feedback: If requested changes are necessary, implement them and push new commits to the feature branch:
git add .
git commit -m "Address code review feedback"
git push
13. Merge the Feature Branch
Once the PR is approved, merge the feature branch into
develop
(ormain
).If using the Git hosting platformβs UI, complete the merge there. Alternatively, merge locally:
git checkout develop
git merge feature/new-feature
git push origin develop
14. Delete the Feature Branch
- After merging, delete the feature branch both locally and on the remote repository to clean up.
git branch -d feature/new-feature
git push origin --delete feature/new-feature
15. Deploy and Verify (Optional)
- If your project has a CI/CD pipeline, the feature might be automatically deployed. After deployment, verify that the feature works as expected in the staging or production environment.
Top comments (1)
Great post!
For anyone who wants to learn more, check out this free ebook here:
bobbyiliev / introduction-to-docker-ebook
Free Introduction to Docker eBook
π‘ Introduction to Docker
This is an open-source introduction to Docker guide that will help you learn the basics of Docker and how to start using containers for your SysOps, DevOps, and Dev projects. No matter if you are a DevOps/SysOps engineer, developer, or just a Linux enthusiast, you will most likely have to use Docker at some point in your career.
The guide is suitable for anyone working as a developer, system administrator, or a DevOps engineer and wants to learn the basics of Docker.
π Download
To download a copy of the ebook use one of the following links:
Dark mode
Light mode
π Chapters