#git#github#ReviewGit#vcs#sourceControl
After installing git check the version to see if you are good to go.
Using git for the first time
The first time you use git , you have to make configuration You have to specify :
- 🔖 The Name
- 🔖 The Email
- 🔖 Default Editor
- 🔖 Line Ending These settings can be specified at three different levers
- 🔥 System Level
- 💡 Applied to the whole system
- 🔥 Global
- 💡 All repositories for the current user
- 🔥 Local
- 💡 The current repository
System Level
On windows, end of lines are marked with \r or \n where \r is Carriage Return and \n is Line feed, while in linux and mac os it is \n
Editing our configurations
Sometimes you want to configure the configuration to your needs, you would want to open the text file where they are written and manually edit them or simply type
User Level
Project Level
Before working with git you must set these up to be able to make commits
Set First configuratiosn
Configure Username
Setting up Email
Setting up username
Check your configurations
Before startin to work with git, check your configurations
Best Practices with Commit Message
- ✅ A short Line Summary (Less Than 50 Characted Optionally Followed By a blank line and a more complete Description
- ✅ Be clear And Description
Example of a good and back commit
-
[?] question
-
[p] Good “Add Missing Hyphen in project section of html”
-
[?] bad: “Update login code”
-
[p] good: “Change User Authentication to use Blowfish”
Example OF a good Commit
ghi2394 - Fixes bug in admin logout
When an admin logged out of the admin area, they
could not log in to the members area because their
session[:user_id] was stil set to the admin ID.
This patch Fixes the bug by setting session[:user_id]
to null when any use logs out of any area.
VIEW COMMITS
Get the 5 Last Logs
Show commits applied since the specified date
Show commits applied until the specified date
Commits from a particular author
Search For a commit message
Last two commits that happened before September 10, 2020?
Which will output the log for all of Karen’s commits labeled “refactor” during March 2019
THE THREE TREE ARCHITECTURE
TAKE A LOOK AT MODIFIED FILES (diff program)
View on working directory
- By default compare the working directory againt the stagging tree
- The git diff command shows the differenct or the things added to a certain file
- 💡 Shows us what was added and what was changed in the file , just a snapshot of the things though
View On stagged file only
Deleting a file in git
When you delete a file manually (normal delete), you will need to use the:
Renaming and moving files
It will show that one was deleted and another one was added
but if you add them to stagging , it will dhow you that it was actually a rename
git mv second_file.txt primary.txt
giv mv second.txt folder/newname.txt
commits
To commit your files you are going to use the git commit command
check a commit , see what is inside a commit
COMPARE TWO COMMITS
CHECK DIFFERENCT BETWEEN TWO COMMITS
You can use the: git diff d94f9f449..f91d45g81 --color-words
Short commit
Checking one line commit of your codes
MAKING ATOMIC COMMITS
To make good commits you must respect the following
-
Small commits
-
Only affect a single aspect
-
Easier to understand, to work with , and to find bugs
-
improves collaboration
-
Make commits to data that are only realated, as in only commit files or data in an ATOMIC way
Do not make a bunch of changes and make single commit, It's really annoying and the more you get advanced it will become harder to debug,
Tagging
#tag sometimes we need to bookmark some point in our commits, like the commit that represent a given version , we use tags for this version .
Branching
- 🔥 Using branches
- 🔥 Compare Branches
- 🔥 Merge Branches
- 🔥 Resolve Conflict
- 🔥 Undo A faulty Merge
- 🔥 Essential Tools(Stashing,Cherry Picking)
Branching allow us to diverge form main land of work and work on something else in isolation, a branch can be thought of a separate isolated workspace.
- We work in an isolated space, make sure everything is working
- Merge our changes when done.
The Way git manages branches.
The way git manages branches, it is very different from other version control system. A branch is simply a pointer to a commit The master branch is simply a pointer to a commit , as you make new commit git moves automatically to the new branch.
Creating a new branch
To create a new branch you simply type
Switching to a branch
To be able to work on your branch you have to move to it, to change to your branch you use the switch command the checkout command used to work , but it had a lot of applications so switch is the new way
Renaming your branch
Sometimes you need to rename your branch and for that you use the -m flag
Code you have in a branch are isolated, if you switch to another branch then you will the old code excluding the branch one
Viewing commits across different branches.
To view commits across different branches you simply use the —all flag
Deleting a branch
Sometime when you are done working on a branch you need to delete it. and for that it is very simple you use the -d flag.
When you want to delete a branch , and it happens to have commits they will tell you that it is not merged. you won't be able to delete it. great safety.
If you are really sure not to need a branch then you can simply use -D to delete the branch.
Comparing branches
Sometimes we need to see how branches are diverging from our main or master branch. we need to know the commits and all those kings of stuffs.
See commits in one branch not in the other
sometimes you want to see commits in the detached branch not in you master branch. you will use a command like this.
Getting the actual changes and not the lists of commits
You want to see the actual changes in a branch ? well yes you can do that too using diff.
Stashing
When we switch branches, git resets our working directory to the snapshot stored in the last commit of the target branch.
If we have changes in our working directory that we have not committed yet, these changes could get lost. So git does not allow use to make changes.`
error: Your local changes to the following files would be overwritten by checkout:
**audience.txt**
Please commit your changes or stash them before you switch branches. Aborting`
Scenario
- 💡 You are working on your master branch
- 💡 You wat to switch to your new branch called Bugfix
- 💡 But you have untagged files in your working directory
- 💡 You don’t want to commit them cause you are not done yet. This is where stashing comes into play
Stashing our changes means storing it in a save place, store it in our git repository but it wont be part of our history
Creating our stash
New Untracked files are not included in your stash by default. To include it you have to use the --all -a flag
View or list your stash
#stashlist#list To view of list all your stashed you are gonna use the list command
Examine a stash, See what is in a given stash
You are back to a given branch and you would like to bright the stashed changes back to your working directory. but before you would wanna see the content of that stash
Apply stash to our working directory
You now want to move changes from your stash to your working directory? yes you can do that .
Removing your stash
You have applied your changes and no longer need your stash ? then yes you can remove them.
You can remove all your stashes too with
Merges (Merging your branches)
In git we have 2 types of merges
- Fast Forward merges
- 3-ways merges
Fast Forward Merge
Used if branches has not diverged
We Create a branch and we apply changes to that branch and we now want to merge the changes from that branch to our master branch. In fast forward we will simply move the master pointer forward to the merge we desire.
3 Ways merge
Used if branches has diverged
You create you branch bring changes to your branch and after that you simply you do the same care to the master branch . you can not do fast ford otherwise you will loose the changes in the master from your branch.
You will do a 3ways marge to achieve the desired effect.
Merging you branches
You want to check whether you have a linear / non diverging merge
Merge using fast forward
You can can merge disabling the fast forward
You ca be working in a company that has a no fast forward policy you can simply disable the fast forward like this:
git config ff no
in your current settings/repo
Deleting A branch in git.
As a best practice , we should always delete our merged branch.
Sometimes you might have a list of branches you merged then forget about it and leave them hanging .
See the list of merged branches.
to view the list of branch we have merged in the master we use
To view the list of unmerged branch we use
Delete a branch
Merge Conflicts
In the real world, sometimes wen we merge changes, we get conflicts. they occur when
- Same codes is changed on different branches in different places.
- Changed in one branch and deleted in another
- Same file is added twice in two different branches and the content are not the same.
You can manually merge the changes, which is simply by manually accessing the file and add remove or delete what is not needed then you save your change.
Aborting a merge.
#abbort Let’s say you want to merge and you get a hundred conflicts, what if you want to revert the merge and first deal with some stuffs and come back to the merge later ? well yo can do that, you can undo the merge .
Undoing a faulty merge.
sometimes we do a merge and find out that our code does not compile or our application does not work. that happens if we screw the merge. In situations like this, we need to undo a merge and remerge.
- First way is removing a commit/log
- here you are rewriting history, and when doing so, you surely don’t want that if you shared your codes online already.
- Reverting a commit approach, we will simply create a new commit that will cancel the changes.
Removing the last commit
When you made a bunch of nonsense commit, you might wanna remove the last commit.
If you have shared you commits, eg: github with some people instead of resetting you can instead undo the changes to the state you wish. much more safer.
Reverting a commit
Squash merging
You create your branch and make a bunch of commit in there and you want to merge now, but here is a thing, you don’t want to merge with all these commits not to pollute your master branch what can you do ? squash merging it is a commit that combines commits from a given branch, all the changes for that , applied to master, it is not a merge, as for it does not have a parent
When doing squash merge it is simper important to really remove that branch or it will create a real confusion in the future, cause if you use
git branch --merged
it will not show as merged yet you technically merged the change in the main
Because squash merge is technically not merged, deleting your branch has to be forced
git branch -D bug.
Rebasing
Another technique for bringing changed into another branch is Rebasing
Rebasing Rewrites History, thus do not use it with codes you have shared.
Cherry picking
You have a feature branch with two commits F1 and F2 one of these commits have some interesting changes we want to have in master, it is possible we can simply take that commit and put it in master.
Picking files from another branch
If you are not interested in a commit then you can simply pic a file you need, and you can do that with a cherry pick
Collaboration
Github is used for collaboration.
Cloning a repository
Listing the remote repositories
A remote repository is a repository that is not on our machine. In our case it is the link git uses when talking to that repository.
Download or fetch changes from remote
#fetch#pull You want to download changes to your remote repository? well you can do that with the fetch command
When you fetch a repository, what happens is that it simply brings the commits but your head still points to the main. you are gonna have to manually merge.
We have pulled the changes and we want to know if we are being of how many commits
We have fetched our data, we know what we are behind for sure and we want to merge to be up to date with the origin.
Pull command = Fetch + Merge
pull=fetch + merge To bring the changes from remote to local , most of the time we wan to do fetch followed by a merge. and we have a command for that pull
Pushing
We are in our local repo and it is ahead of the origin/main we want to push our change to the remote repo.
Pushing our Tag
By default pushing does not push your tags, you should push them individually.
Deleting our tags from origin
Pushing our branches
Now you want to work with branches and push them to github. well easy, you create them and push them (how do yo push them ? )
If you run
git push
while in your new branch, then you will get some error. saying that the branch you are trying to push is not linked to a branch in the origin.
If you want to push a branch for the first time you have to set the upstream branch (remote branch) with the -u flag.
You are done with your branch and you would simply want to delete it from remote
Delete a tracking branch that are not on remote
sometimes you have a remote tracking branch tracking changes of a given branch in the remote . you can remove automatically these branches using the prune command.
Pull requests.
Quite often we would want our team members to help us reviewing our codes with a pull requests. with a pull request we essentially open a discussion with the team before merging to the main branch.
The Remote Command
We Use the git remote command to manage our remote repositories.
Adding a new remote
To add a new remote it is as easy as using the add command
Rename a remote repository
To rename a remote repo we sue, git remote rename command
Removing a remote
Synch changes from upstream to local
You would want to get updates of a particular repo you forked. what are the steps ?
Ignore Tracked Files
#gitignore If you don’t want to track a file you have to add them to your git ignore
Sometimes you create your git ignore after you have tracked some changes in a particular file , if you add that file to .gitignore later on, it won't still be tracked you need to remove the file to the cache
Guide to remove tracked files
When you’ve already tracked files that you later add to your .gitignore
file, Git might not automatically stop tracking those files. This is because Git’s tracking of files is based on their history. If a file has already been committed and is part of the history, adding it to .gitignore
won’t immediately remove it from the repository.
Remove Cached Files
First, you’ll want to remove the files from the index and cache. You can do this using the following command:
Commit Changes
After removing the files from the cache, commit the changes to the repository:
Update .gitignore
Ensure that the files you want to ignore are correctly listed in your .gitignore
file. If not, add them to the file.
Commit Again:
Commit the updated .gitignore
file:
Finding a bad commit using git bisect
Divide history in half and get to test and see where there is a problem in our codes
Finding contributors
#contribution#contibutor#authors
git ls-tree HEAD
============
List all files on the current directory
Git Tracking Directories
In the directoty you want to track simply add a
.gitkeep and your directory will start being tracked
ADVANCED GIT
Point to the latest commit,
git show HEAD
-Ancestry: Parent (^)
Reference the direct parent aka commit right after the current one
we use the symbo (^)
git show HEAD^ : will show the previous commit right after the head
or git show master
or git show HEAD~
or git show HEAD~1
Multiple Parents
you simply use multiple ^^…
git show HEAD^^…
git show HEAD~2
Show directories and file in a tree-ish
Just like an ls command
git ls-tree HEAD
git ls-tree HEAD assets/
git ls-tree HEAD
040000 tree a20f5c1eb5270ad88529db701d6ba790ad19f50e notneeded
100644 blob a8f3da1151c71091c0521ae4c489cce3eed5ac94 second_file.txt
100644 blob 8a56ec4382c359ce58dc65543c9cefc402bbbf0b thirt.txt
Where blob:bigLongBinaryFile
Tree: Directory
FILTER THE LOG
limiting Commits:
git log -3#first 3 Commits
—since and —until,—after,—before
git log —since=2019-02-29
git log —until=‘3 days ago’
git log —after=2.weeks —before=3.days
Filter by author
git log —author=‘danielerat’
Look for a commit message
git log —grep=“Initial”
look for commit within a specific range
git log b447gh12..HEAD
!!!!!!!!!
Git logs of a particular file
git log first.html
*You will only get changes that afect that file
FORMAT THE COMMIT LOG (git log -p)
You can brownse through different commits and see changes applied to them
git log -p
git log —stat
Brings the statistics of the logs
something like this
commit e3806740d77048ac8381cff5adaab8ff1f8a9583
Author: danielerat davidodo2015@gmail.com
Date: Sat Jun 11 09:06:10 2022 +0200
Modifying content of the third file
thirt.txt | 3 +—
1 file changed, 1 insertion(+), 2 deletions(-)
Specifying the format of logs
git log —format=YourFormat
Formats are: oneline,short,medium(default),full,fuller,email,raw
or You can use
Show format of one line commits
git log —oneline
Using Graphs
Graps are useful when dealing with branches
git log —graph
or even better use
git log —graph —all —oneline —decorate
BRANCHES OVERVIEW
- 💡 Branches are cheap, easy to create
- 💡 easy to delete
- 💡 try new ideas
- 💡 isolate features or sections of work
- 💡 fast context switch master_branch
Listing BRANCHES
To list all branches simply use
By default all git projects are on the main Branch
Creating new branch
Renaming a branch
Comparing the branches
Detached head
Sometimes you want to move back in time and see the commits on a specific branch, for that you can use the checkout command,
Check Where the head Points to
When you create a new branch by default git is pointing to that new branch
!!! If you type : ls -la .git/refs/heads/
You will see the list of all branches
!!!!! As Soon as you are done creating a new branch they will be pointing to the same
commit as the master,
git log —oneline
df3d3aa (HEAD -> master, new_feature) Adding another line in the third file *random line
Stashing
The git stash command takes your uncommitted changes (both staged and unstaged ), saves them away for later use, and then reverts them from your working copy
Viewing the merged branch
Rebasing
Cherry picking
Collaboration
Cloning a repository
Syncing with remotes
Sharing tags
Sharing branches
Managing remotes
Tip
Get Commits for a particular file
Restore Deleted File
#remove#delete You delete a file then add and commit and you would first want to see the commits made on that file ? well use the — flag
Now to restore the file you simply have to checkout the previous commit where the file existed and simply restore that specific file
The Git Reset Command
The git reset
command is a crucial tool that allows you to manipulate commits, branches, and the staging area.
What does it do?
How Git Reset Works Behind the Scenes
When you run
git reset
, it adjusts the “HEAD” pointer and potentially other pointers to modify the state of your repository.
Modes of git reset
The
git reset
command has three primary modes, each with distinct effects:
- Soft Reset: This mode resets the HEAD pointer to a specific commit without modifying your working directory or staging area. It’s often used when you want to rewrite commit history before pushing changes to a remote repository.
- Mixed Reset (Default): This mode resets the HEAD pointer and updates the staging area to match the specified commit. It leaves your working directory untouched, allowing you to review and modify your changes before committing them again.
- Hard Reset: This mode resets the HEAD pointer, the staging area, and your working directory to match the specified commit. It discards all uncommitted changes, so use this mode cautiously.
How to Use Git Reset
The basic syntax of the git reset
command is:
where
<mode>
: Specifies the reset mode (--soft
,--mixed
, or--hard
).<commit>
: Specifies the target commit to which you want to reset.
soft reset
This resets the HEAD pointer to the previous commit, preserving changes in the staging area and working directory.
Mixed Reset (Default):
This resets the HEAD pointer and the staging area to two commits back, leaving changes in your working directory.
Hard Reset:
This resets the HEAD pointer, staging area, and working directory to the commit with hash "abc123."
When to Use Git Reset
- Undoing Commits: You can use
git reset
to remove or modify commits. Soft reset is useful for rewriting commit messages, while mixed or hard reset can be used to remove commits from history. - Unstaging Changes: When you’ve staged changes but want to unstage them, use
git reset
with the mixed mode. - Starting Over: In cases where your working directory is in an undesirable state, a hard reset can revert everything to a specific commit.
Advantages of Using Git Reset
- Granular Control: Git reset provides precise control over which changes you want to reset and how.
- Safe Rewriting: Soft and mixed resets allow you to revise your commit history before sharing changes with others.
- Flexible Workflow: Git reset fits various workflows, from cleaning up commits to preparing feature branches for pull requests.