Git is a widely used version control system for tracking code changes in software development.
Developed by Linus Torvalds, it offers powerful features for collaboration, code management, and project history.
With Git, developers can work locally, experiment, and easily revert changes. It allows precise control over code evolution and smooth collaboration through merging, conflict resolution, and branch management. Git's distributed nature enables offline work and minimizes reliance on a central server.
Its branching model promotes structured workflows and concurrent development. It provides a rich set of tools and commands for version control operations, and it has a thriving community and integrations with popular hosting platforms.
Whether for personal or team projects, Git is an essential tool for efficient and collaborative software development.
When writing new code, create a branch with the new feature, and when finishing the feature merge it into original (often master) branch and then delete the feature branch.
Here are some tips to help you effectively use Git:
Here are useful git bash commands.
git clone <url>
git status
git branch <new_branch>
Create branch and switch to it
git checkout -b <new_branch>
This is shorthand for:
git branch <new_branch>
git checkout <new_branch>
git checkout <branch>
git show-branch
git diff
use arrow up/down to scroll, and press q to quit.
see difference in file:
git diff <file>
Compare branches:
git diff <branch1> <branch2>
git pull
git push or git push <repository> <branch>
git add <file>
add all files to staged files in current directory:
git add .
Unstage file:
git restore --staged <file>
Unstage all files
restore --staged
git checkout -- <file>
Discard all changes in all files
git checkout -- .
git reset HEAD -- <file>
Remove directory from staging area
git reset HEAD -- <directoryName>
git commit -m "changed some things"
git update-index --assume-unchanged <path>
git log
If you wish to undo/revert the last commit you can do the following, using the commit hash that you get from the git log command:
git revert <commit hash>
To remove the last commit from git, you can simply run git reset --hard HEAD^.
If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits.
You can increase the number to remove even more commits.
Then, force a push (git push --force origin or git push --force origin master should be enough).
git config --list will list the settings. There you should also find the email of the user.
run git checkout master to change the active branch back to master.
run git pull.
run git merge <new-branch> to merge the new feature into the master branch.
Note that git merge merges the specified branch into the currently active branch. So we need to be on the branch that we are merging into.
Run git push.
If there are conflicts, solve them.
git checkout feature-branch
git rebase master
Powerful command that enables arbitrary Git commits to be picked by reference and appended to the current working HEAD.
git cherry-pick <commit-hash>
Delete locally :
The -d option stands for --delete, which would delete the local branch, only if you have already pushed and merged it with your remote branches.
The -D option stands for --delete --force, which deletes the branch regardless of its push and merge status, so be careful using this one!
"git branch -d branch_name"
"git branch -D branch_name"
Delete remote:
"git push <remote_name> --delete <branch_name>".
git commit --amend -m "New commit message"
A good folder structure is very important for locating and accessing files easier.
Then the developers can work faster.
The root should be reserved for configuration files, documentation (such as .gitignore and others).
Also, it can contain VS solution files, git files and ".env.example" file to show the used environment variables (so the developer can set them in his computer locally).
This is where all source files are placed.
However, in languages that use headers (or if you have a framework for your application) don't put those files in here.
This is the directory where all your libraries/dependencies/ files to include should be stored.
Also, if you have your project in multiple files, put your headers and attached source in here.
Documentation goes in here.
For example, "docs.md" file.
A less common one.
For all static resources in your project.
For example, images and audio.
Convenience directory for your use.
Should contain scripts to automate tasks in the project, for example, build scripts, rename scripts.
Usually contains ".sh", ".bat" files for example.
The place where your built files will go.
Usually split into two directories, Debug and Release, it can contain binaries, .DLLs and any compiled files.
It may also contain build scripts, like makefiles, but they should generally be in the root.
Contains all tests.
tools and build will have prod and test directories.
root/test will have directories - test_name that will have lib, src, res directories.
Store modules (unless the framework use it elsewhere).
MD files (Markdown documentation) are used to lightweight markup language.
Common syntax is:
You can create a file called file_structure.md and store it in doc folder and explain about the folder structure.
Here is template for "README.md" file:
One paragraph of project description goes here.
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.
What things you need to install the software and how to install them.
```give examples``` (Store this in special code frame).
A step by step series of examples that tell you how to get a development env running
Say what the step will be:
```give examples``` (Store this in special code frame).
And repeat
```until finished``` (Store this in special code frame).
End with an example of getting some data out of the system or using it for a little demo.
Explain how to run the automated tests for this system.
Explain what these tests test and why
Here is example for "file_structure.md" file:
Information about the folder structure of the project.
Binary build files created from makefiles.
Documentation files about the project.
Libraries used in source code.
Object files created when compiling source code files using makefiles.
Resources used by source code.
Source code of the prod version of the project.
Test source code used to test the project.
Tools used to run the project.
If you prefer using Git with a GUI (graphical user interface) over command-line operations, you can use these tools:
Effective task management is crucial for organizing your work, staying productive, and meeting project deadlines. In the next step, we delve into various task management techniques and tools that help you plan, track, and prioritize your tasks effectively.`