$ git init [directory]
Example:
$ mkdir myproject $ cd myproject $ git init
This creates a new Git repository in the myproject
directory.
- git clone
The git clone
command creates a copy of an existing Git repository. This command downloads the entire repository, including all its files and history, onto your local machine.
Usage:
$ git clone [url] [directory]
Example:
$ git clone https://github.com/user/repo.git myrepo
This clones the repo.git
repository from https://github.com/user/repo.git
into a new directory called myrepo
.
- git add
The git add
command adds files to the staging area. The staging area is where you prepare your changes before committing them to the repository.
Usage:
$ git add [file]
Example:
$ git add index.html
This adds index.html
to the staging area.
- git commit
The git commit
command saves your changes to the repository. This command creates a new commit with a message that describes the changes you made.
Usage:
$ git commit -m "commit message"
Example:
$ git commit -m "added index.html file"
This creates a new commit with the message "added index.html file".
- git push
The git push
command uploads your local repository to a remote repository. This command is used to share your changes with other developers.
Usage:
$ git push [remote] [branch]
Example:
$ git push origin master
This uploads your local master
branch to the origin
remote.
- git pull
The git pull
command downloads changes from a remote repository and merges them with your local repository.
Usage:
$ git pull [remote] [branch]
Example:
$ git pull origin master
This downloads changes from the master
branch in the origin
remote and merges them with your local repository.
- git status
The git status
command shows the current status of your repository. This command shows which files have been modified, which files have been staged, and which files have not been tracked by Git.
Usage:
$ git status
Example:
$ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: index.html Untracked files: (use "git add <file>..." to include in what will be committed) style.css
This shows that index.html
has been staged and style.css
is an untracked file.
- git log
The git log
command shows a history of the commits in your repository. This command shows the commit message, the author, the date, and the commit hash.
Usage:
$ git log
Example:
$ git log commit 3ce4f4f4ef4e6b5d9e719416b6e5e6c5e6d5f6e4
Author: John Doe <john@example.com> Date: Wed Mar 31 12:34:56 2021 -0700 added index.html file commit 2a3b4c5d6e7f8a9b0c1d2e3f4g5h6i7j8k9l0m1n Author: Jane Doe <jane@example.com> Date: Tue Mar 30 12:34:56 2021 -0700 initial commit
This shows a history of the two commits in the repository.
- git branch
The git branch
command shows a list of branches in your repository. This command also allows you to create, rename, and delete branches.
Usage:
$ git branch [branch]
Example:
$ git branch feature
This creates a new branch called feature
.
- git checkout
The git checkout
command allows you to switch between branches and restore files to a previous commit.
Usage:
$ git checkout [branch or commit] [file]
Example:
$ git checkout feature
This switches to the feature
branch.
$ git checkout 2a3b4c5d6e7f8a9b0c1d2e3f4g5h6i7j8k9l0m1n index.html
This restores the index.html
file to the state it was in the 2a3b4c5d6e7f8a9b0c1d2e3f4g5h6i7j8k9l0m1n
commit.
Conclusion
These are some of the most popular Git commands that developers use on a daily basis. Understanding how to use these commands is essential for effective collaboration and version control. With these commands, you can easily manage your codebase, collaborate with others, and keep track of changes over time.
0 Comments