In this session, I will give you a tour of the important features of Git. This tool allows you to do versioning and to keep track of the project modifications
Git is an open-source Distributed version control systems used for tracking project changes and revisions across different teams.
Git saves different versions of projects in a folder known as a Git repository.
A Git repository is a virtual storage of your project. It allows you to save versions of your code, which you can access when needed.
The git init command allows to initialize a new empty git repository. This will create a .git folder which will contain the information about our versioning.
cd my-project
git init
Before starting to do anything it is important to configure git. So we will have to configure user information. This information will be visible in the history and will allow to know later who did what.
git config --global user.name "My_Name"
git config --global user.email "[email protected]"
git status
This command allows to get the state of the history,
git add <file-name> //add file name to the staging area
git add --all //add all the files to the staging area
A particularity of git is its staging system which allows you to select the files to follow during the next commit. You can think of it as a "waiting area" where you list the files you want to be saved.
git commit -m "message to describe your commit"
Make a commit A commit is a step in the history of your project, a step that we can identify with a particular message.
git log
The log command allows us to obtain information on the different commits of our project.
git log --oneline
Allows to display the history with one line per commit example:
git log --oneline
42f8151 fix bugs fa1c031 redesign b93b035 add a style file fb7597a first commit
git checkout <commit-id>
In short you have gone back in time as a viewer. You can see the project as it was at the time of the commit while having the ability to return to the "present". We use this command to watch old commits,
git checkout fa1c031
return:
Note: switching to 'fa1c031'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by switching back to a branch.
git checkout master //if you want to go back to the actual state
git revert <commit-id>
This command will undo what was done at the time of the commit by creating a new commit. This does not alter the history but will add a new reversal commit
git revert fa1c031
result:
Removing filee.txt [master acbb7e8] Revert "redesign" 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 filee.txt
the file that i was added in the "redesign commmit" will be removed and a new commit will be created
git reset
Deletes all files from the staging area, without deleting the changes.
git reset <commit-id>
- Allows you to go back to the commit,
- Resets the staging area while leaving your working folder as is. The history will be lost (subsequent commits will be lost, but not your changes).
This command mostly allows you to clean up the history by resubmitting a single commit instead of too many scattered commits.
Example :
git reset fa1c031
git reset <commit-id> --hard
- Returns to the
<commit-id>
- Reset the staging area and working folder to match.
All changes, as well as any commits made after the
<commit-id>
will be deleted. Use with extreme caution
A branch in Git is a way to keep developing and coding a new feature or modification to the software and still not affecting the main part of the project. We can also say that branches create another line of development in the project. The primary or default branch in Git is the master branch
git branch # Allows you to list the branches
git branch branch_name # Allows you to create a new branche branch_name
git branch -m branch_name # rename the current branch to branch_name
git branch -d branch_name # delete a branch
The branch command allows you to manage everything related to branches (adding, listing, removing, renaming)
git checkout branch_name
Allows you to go to an existing branch.
Example:
git branch my_first_branch #create my new branch
git checkout my_first_branch #switch to my new branch
git add .
git commit -m "my first commit in my new branch" #add a commit in this branch
Result:
Switched to branch 'my-first-branch'
git merge branch_name
Merge allows you to connect two branches together and fuse them. The fusion of two branches is always done from the main branch.
Example:
git chekout master # we must switch to the main brach
git merge my-new-branch
git remote add <alias> <chemin/url> # Add a new remote repository
The remote command allows you to create, display and delete connections. These connections should be considered as simple aliases of the real repository path.
Exemple:
git remote add origin https://github.com/khaliljedda/git-session.git
git push <remote> <branche>
The push command is used to transfer local commits to the remote repository.
example:
git push origin master
The fetch command allows to import the information from the remote repository. The import is done through special branches to give us the possibility to compare it and if necessary merge manually.
git fetch <remote> # Retrieves all branches and commits
git fetch <remote> <branche>
The pull command allows you to do a git fetch followed by a git merge in a single command.
git pull <remote>
git pull <remote> <branche> # pull from a specefic branch