Git is a popular version control system. It was created by Linus Torvalds in 2005, and has been maintained by Junio Hamano since then.
It is used for:
- Tracking code changes
- Tracking who made changes
- Coding collaboration
- Initialize Git on a folder, making it a Repository
- Git now creates a hidden folder to keep track of changes in that folder
- When a file is changed, added or deleted, it is considered modified
- You select the modified files you want to Stage
- The Staged files are Committed, which prompts Git to store a permanent snapshot of the files
- Git allows you to see the full history of every commit.
- You can revert back to any previous commit.
- Git does not store a separate copy of every file in every commit, but keeps track of changes made in each commit!
- Over 70% of developers use Git!
- Developers can work together from anywhere in the world.
- Developers can see the full history of the project.
- Developers can revert to earlier versions of a project.
- the largest host of source code in the world, and has been owned by Microsoft since 2018.
- It's not the same as Git but it uses Git.
sudo apt update
sudo apt install git
git --version
Use global to set the username and e-mail for every repository on your computer.
git config --global user.name "Muhammad Sabry"
git config --global user.email "[email protected]"
mkdir GP
cd GP
git init
To check for any changes in the tracked directory.
git status
- adding single file.
git add script.py
- Adding more than one file using regex.
git add *.png
The previous command will add all unstaged pictures with png extension in the current directory.
- Adding all changes.
git add --all
git add -A
git add .
Using --all instead of individual filenames will stage all changes (new, modified, and deleted) files.
Temporarily shelves changes you've made to your working copy so you can work on something else, and then come back and re-apply them later.
- Stashing Unstaged changes.
git stash -u
- Stashing Staged changes.
git stash
- Bringing back Stashed work.
git stash pop
Popping your stash removes the changes from your stash and reapplies them to your working copy.
- Alternatively, you can reapply the changes to your working copy and keep them in your stash.
git stash apply
- Manging multiple Stashes.
git stash list
- Adding a message to the stash.
git stash save "A brief message"
- Popping a specific Stash form Stash list.
git stash pop stash@{index}
- Dropping a specific Stash.
git stash drop stash@{index}
- To clear your Stash.
git stash clear
Since we have finished our work, we are ready to move from stage to commit for our repo.
- Adding commits keep track of our progress and changes as we work. Git considers each commit change point or "save point".
- When we commit, we must include a message (Mandatory).
git commit -m "A brief Message"
The commit command performs a commit, and the -m "message" adds a message.
- Adding Description with the Commit Message.
git commit -m "A brief Message" -m "Description ........"
git status
git commit -am "A brief message"
The -am allows us to add and message at the same time.
Note: to use -am you must check the status first and it's working with already added files not new created ones.
Check the status of our repository. But this time, we will use the --short option to see the changes in a more compact way:
git status --short
- ?? - Untracked files
- A - Files added to stage
- M - Modified files
- D - Deleted files
Skipping Staging Environment is not generally recommended.
Allows us to view the history of commits for a repository.
git log
To avoid the very long log list, it's better to use --oneline displaying every commit with it's hash and message.
git log --oneline
It simply displays the changes between two commits
git diff #commit1hash #commit2hash
Note: change the order of the commits affects the result as it displays the changes from commit1 and commit2.
A Branch is a new/separate version of the main repository.
Using Branches allows you:
- Working on a new version without impacting the live version
- Creating a new branch to fix small errors then merge it to the live version.
- divide the work between more than one person working on independent units.
- You can switch between different Branches and work on them without impacting eachother.
git branch NewBranchName
git branch
NewBranch
* master
the * refers to that we are currently on that Branch.
To navigate between Branches
git checkout NewBranch #Old Command
git switch NewBranch
That command switches us to NewBranch.
git checkout -b MyNewBranch
Using -b on checkout will create a new branch if it doesn't exit then it will navigate to the new created branch.
First we need to navigate to our destination branch which will have the merged version and it's usually the master branch .
Then we merge the master branch with NewBranch.
git checkout master
git merge NewBranch
Since we finished the work on NewBranch we can delete it.
git branch -d Newbranch
Merge Conflict happens when there's two versions of the same file in the master and the other branch due to commits on the destination branch to fix the conflict we edit the file with the conflict in the destination branch then we run git commit that will conclude the merge.
This Command is used when we want to take a previous commit as a new commit.
-
Find that previous commit.
-
Calculate the number of steps.
-
Make it the new commit.
git log --oneline
If it's the previous commit
git revert HEAD --no-edit
Adding --no-edit to replace the commit message with default revert message.
To revert to an earlier commit, use HEAD~x where x refers (Number of steps -1).
git revert HAED~2 --no-edit
It will make the 3rd previous commit the new one.
reset is the command we use to move the repository to an earlier commit without making new commits.
- Find that previous commit.
- Get that commithash
- Move the repository back to it.
git log --oneline
Get the hash, then
git reset 9a9add8
Even if the following commits are no longer displayed in the log but they still exist.
Store the commithash before applying the reset so you can get back to any commit.
git reset e56ba1f
So reset could be used to go backward or forward.
It combines the latest staging changes with the latest commit as a new commit replacing the old one.
Usually used to change the latest commit message.
git commit --amend -m "New message"
.gitignore file allows us to ignore the changes of specific files as it uses regex to ignore this group of files and folders.
-
After intializing the repository, create .gitignore file.
-
Adding the regex of wanted untracked files.
Note: Search in google for the .gitignore file for the programming language that will be used in that project, copy it then paste in the created .gitignore file.
nano .gitignore
To see all possible commands.
git help --all
Or through