GIT
Summary
Git
Git is a distributed version control system used for tracking changes in source code during
software development. It was created by Linus Torvalds in 2005 and has since become one of the
most widely used version control systems in the software development industry.
Something that git does is that it devices projects to several parts.
Standard directory -> Changing files ?-> stage ( آنجایی کە میخواهی کومیت کنی، )لبە->commit
After steps followed above, a new standard directory is created. This new standard directory
becomes the main standard directory.
o Imagine you have a file in a directory and git prompt is also open on that directory.
Now, imagine that you have done some changes to that file and saving it.
Git init
It is a command used to initialize a new Git repository in an existing directory.
git status: (tells about the current status of the git folder and the files in it)
After doing some changes to a file, when you type this command. git will tell you that the
file is untracked, meaning that the changes hasn't been saved to the project on git.
git add filename.txt
So, the file should go to the stage mode. This makes the file to be tracket again.
Note: If you want to stage all the files in that directory, you can write ‘git add -A’.
or you can type several file names like ‘git add filename.txt filename2.tex’
Note: if you add some new files to the folder, those files are untracked because git is not
taking care of them. So, if you use the “git status” command, you’ll find out that those
files are untracked. Now, to make sure that git takes care of those files too then you
should “git add” them which in esense means that they are new. So, adding them will push
them to the “staging”. Now, you can commit them.
At the staging, you either have files that are new or changed.
git status:
Now, that we dit “git add”, the file is brought to the stage mode, and is reaty to be
commited.
git commit -m “tell something about the changes you made”
Now the file is commited. If you know use ‘git status’ command, you will see that there is
no file that is untracked.
o Editing
o Checking the changes
git log
shows your history activity.
o Checking the changes
git diff HEAD
This will show the differences between the current state of your working directory and
the latest commit in the repository.
Why Head? Because Head means the last commit that you have done.
git diff - -staged or git diff - -cached
Both commands will achieve the same result, showing the differences between the
staged changes and the latest commit.
git reset - - page3.html:
Used when something went wrong so that the files are taken out of the stage
environment. This command is used to unstage the changes made to page3.html. If
you previously used ‘git add’ to stage the file but haven't committed it yet, this
command will remove the changes from the staging area, effectively "unstaging" the
file. The changes in the file will still be present in your working directory.
git checkout - - page3.html:
This command is used to revert the changes made to page3.html to the state of the
last committed version (which is head). It discards any modifications made to the
file in the last commit.
o Managing branches in a repository.
These commands are essential for creating, switching between, and managing branches in
a Git repository. Branches are a powerful feature in Git, enabling developers to work on
different features or fixes independently and later merge them back into the main
codebase.
`git branch`: This command displays a list of all branches in the repository. The
current branch is highlighted with an asterisk.
git branch <branch_name>
Creates a new branch with the given name.
git checkout <branch_name>
Switches to the specified branch, making it the current working branch.
git checkout -b <branch_name>
Creates and switches to a new branch with the given name in one step. This is a
shortcut for creating and checking out the branch simultaneously.
git merge <branch_name>
Merges the changes from the specified branch into the current branch.
git branch -d <branch_name>
Deletes the specified branch if it has been merged into the current branch.
git branch -D <branch_name>
Forces the deletion of the specified branch, even if it contains changes that haven't
been merged.
git log --graph --oneline –all
Shows a concise graph of the branch history, displaying all branches in the repository.
o To change a filename in Git
‘git mv’ command to rename the file:
git mv old_filename new_filename
o push and pull changes in a GitHub repository
Push Changes to GitHub (Upload Local Changes to Remote Repository)
a. Make sure you have already initialized a Git repository in your local project directory
using git init.
b. Add and commit your changes using the following commands:
git add .
git commit -m "Your commit message here"
c. Push your committed changes to the remote repository on GitHub:
git push origin <branch_name>
Replace <branch_name> with the name of the branch you want to push to (e.g.,
main, master, etc.).
Pull Changes from GitHub (Download Remote Changes to Local Repository):
a. git pull origin <branch_name>
Again, replace <branch_name> with the name of the branch from which you want
to pull changes.
example:
step 1) Cloning a Remote Repository to your local machine
git clone https://github.com/username/my-project.git
step 2) Note: Understanding "origin"
So, "origin" now points to the remote repository (https://github.com/username/my-project.git)
you cloned from.
Using "origin" as an alias makes it easier to interact with the remote repository.
step 3) Pushing Changes to the Remote Repository:
git push origin <branch_name>
For example, if you made changes to the "main" branch, you'd push the changes to the remote
repository using: git push origin main
This command will send your committed changes to the remote repository on GitHub and update
it with the latest changes from your local "main" branch.