What is Git?
Git is a distributed version-control system for tracking changes in source code during software development. It is designed for coordinating work among programmers, but it can be used to track changes in any set of files. Its goals include speed, data integrity, and support for distributed, non-linear workflows.
At a high-level, Git can be thought of as a timeline management utility.
What are some features of Git (Branches & Merges... in Git)?
→ Some of Git features include:
Branches and Merges, these are only a few known features that allow you to separate changes and/or add them to your original work in a very organized way.
Git provides many useful features for developers and writers and anyone who wants to save and/or well organize his work.
Git is mostly used to:
- Keep track of all files in a project
- Record any changes to project files
- Restore previous versions of files
- Compare and analyze code
- Merge code from different computers and different team members.
Repositories in Git?
Distributed Version Control System: Git has a remote repository that is stored in a server and a local repository that is stored in the computer of each developer.
By using git you are welling to well organize your work, as well as saving version of it.
Versions you can go back to.
these versions of your project will be saved locally unless you were using a Remote repository Like GitHub.
GitHub provides hosting for software development and version control using Git.
for now, we will only use a local repository.
→Steps to do before you Locally Commit any changes to your local repository:
-To install Git on Windows or Mac or Linux, you will need to download the installer from the Git website:
Click and download the most current version for your operating system.
Run the installer to install Git on your Machine.
First, create whatever project you are working on. (e.g application, game, software, website…etc).
Second, inside your project’s directory create a local repository root directory (folder).
Here is How to do that:
- Open the project Directory in GitBash or in your PowerShell in case you were using Windows.
- Run the command
git initto start a new repository.
- Now you can add some code/edits to your project.
Don’t forget to run these commands to register your name and email so the one who is committing changes is Known:
$ git config --global user.name "firstName LastName"
$ git config --global user.email "Email Adress"
You can launch the Git GUI from the bash shell. Type
git gui at the command line and hit enter:
Once you open Git you can select either Create New, Clone Existing, or Open Existing Repository. In this example, we create a new repository. Enter a directory name or click on Browse to navigate to a directory.
To add to Git, you are going to have to use:
git add [file name]
This way you can add whatever file you have just created, or files u have just added and not yet added those edits.
- You can use this Command
git add .to add all files and edits that need to be added.
- Use this Command
git statusto check the status of all your added and not added files, as well as committed and not committed files.
Git acts differently than other Version Control systems. It records the entire contents of each file in every commit.
How to Commit changes in Git?
First, check if all your changes are added and saved.
Then, Commit your changes using this Command
How it works?
git commit command captures a snapshot of the project’s currently staged changes. Committed snapshots can be thought of as “safe” versions of a project—Git will never change them unless you explicitly ask it to.
- Use the Command
git logto view committed changes to your Local repository.
- Instead of only using
git commit, You can use this command:
git commit -m "a message to differentiate my version from other versions"
this way your commit will come with a message you specify.
git commit with no flags:
Commit the staged snapshot. This will launch a text editor prompting you for a commit message. After you’ve entered a message, save the file, and close the editor to create the actual commit.
git commit with -a flag:
git commit -a
Commit a snapshot of all changes in the working directory. This only includes modifications to tracked files (those that have been added with git add at some point in their history).
git commits with -m flag:
git commit -m "commit message"
As seen before, this is a shortcut command that immediately creates a commit with a passed commit message. By default, git commit will open up the locally configured text editor, and prompt for a commit message to be entered. Passing the -m option will forgo the text editor prompt in-favor of an inline message.
git commit with -am flag:
git commit -am "commit message"
A powerful shortcut command that combines the -a and -m options. This combination immediately creates a commit of all the staged changes and takes an inline commit message.
git commit with –amend flag:
git commit --amend
This option adds another level of functionality to the commit command. Passing this option will modify the last commit. Instead of creating a new commit, staged changes will be added to the previous commit. This command will open up the system’s configured text editor and prompt to change the previously specified commit message.
It is a common case where you commit you never wanted to.
git commit -m "Something terribly misguided"
but, you figured out that was not what you wanted to commit.
So How to undo your Commits in Git?
luckily you can do this:
git reset HEAD~
This Command does nothing to your working tree (the state of your files on disk), but undoes the commit and leaves the changes you committed unstaged (so they’ll appear as “Changes not staged for commit” in
git status, so you’ll need to add them again before committing).
After this, you only need to make the necessary changes to your files.
you can use this Command:
git reset --soft HEAD~
If you only wanted to add more changes to the previous commit, or change the commit message.
this last Command is much like
git reset HEAD~ but, but it leaves your existing changes staged.
- The next step is to make the necessary corrections to your working tree files.
- Now add what changes and files you want using the
- Finally commit the changes, reusing the old commit message.
resetcopied the old head to
-c ORIG_HEADwill open an editor, which initially contains the log message from the old commit and allows you to edit it. If you do not need to edit the message, you could use the
See this topic for more details on how to undo changes locally and on a remote repository: How to undo commits in Git?