-
Notifications
You must be signed in to change notification settings - Fork 0
Basic
Git commands are instructions you type into the terminal/command prompt to interact with Git. These commands allow you to:
β
Create and manage repositories π
β
Add and commit changes π
β
Push and pull changes from remote repositories π
β
Work with branches πΏ
β
Undo mistakes and restore previous versions βͺ
| Command | Description |
|---|---|
git init |
Initialize a new Git repository |
git clone |
Copy (clone) an existing repository |
git status |
Check the status of your working directory |
git add |
Add files to the staging area |
git commit |
Save changes to the local repository |
git log |
View the commit history |
git diff |
View differences between changes |
git branch |
Manage branches |
git checkout |
Switch between branches |
git merge |
Merge changes from one branch to another |
git pull |
Fetch and merge changes from a remote repository |
git push |
Send changes to a remote repository |
git remote |
Manage remote connections |
git reset |
Undo changes and reset commit history |
git stash |
Temporarily save changes without committing |
To create a new Git repository in your project folder:
git initβ Output:
Initialized empty Git repository in /path/to/your/folder/.git/
π Success! Git is now tracking your project.
If you want to download a repository from GitHub, GitLab, or Bitbucket:
git clone https://github.com/username/repository-name.gitβ Example:
git clone https://github.com/example/MyProject.gitThis creates a folder called MyProject and downloads all the repository content into it.
To see which files have been modified, staged, or untracked:
git statusβ Output:
On branch main
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
This shows the current status of your files.
To add files to the staging area (preparing them for commit):
git add filenameβ Example:
git add README.mdgit add .β This adds all modified and new files.
A commit saves the staged changes to the local repository. Think of it as taking a snapshot of your project.
git commit -m "Your commit message"β Example:
git commit -m "Add initial project files"π Your changes are now committed!
To view the commit history of your project:
git logβ Output:
commit abc1234567
Author: John Doe <johndoe@example.com>
Date: Mon Jan 1 12:00:00 2025 +0000
Add initial project files
git log --onelineβ Output:
abc1234 Initial commit
def5678 Add README file
To see the changes youβve made compared to the last commit:
git diffβ If youβve staged the changes, compare staged changes with:
git diff --stagedA branch is like a parallel universe where you can make changes without affecting the main project.
git branchβ Output:
* main
feature-branch
git branch branch-nameβ Example:
git branch feature-branchgit checkout branch-nameβ Example:
git checkout feature-branchgit checkout -b branch-nameβ Example:
git checkout -b new-featureTo merge changes from one branch into another:
git merge branch-nameβ Example:
git merge feature-branchThis applies the changes from feature-branch into the current branch.
To upload your changes to a remote repository:
git push origin branch-nameβ Example:
git push origin mainπ Your changes are now on GitHub, GitLab, or Bitbucket! π
To fetch and merge changes from a remote repository:
git pull origin branch-nameβ Example:
git pull origin mainThis keeps your local copy up to date.
Sometimes, you may want to undo your changes.
To remove files from the staging area:
git restore --staged filenameβ Example:
git restore --staged README.mdTo discard all local changes:
git restore filenamegit reset --hard HEAD~1This removes the last commit entirely. Be careful!
If you need to save your work temporarily without committing:
git stashβ When youβre ready to restore your work:
git stash popTo view your current remote repositories:
git remote -vgit remote add origin https://github.com/username/repo.gitgit remote set-url origin https://github.com/username/new-repo.git-
use
$ lsto list out the project folder that you want to track on -
next
cdto the folder -
create a directory for our project
mkdir myprojectand then cd to the directorycd myproject -
after you decided the location of the project use
initto create a git folder in your project
$ git initto initializing empty Git repository in our project directory
NOTE: the .git file that create by init was hidden file and you should not do anything on it.
It will store all the history of our project . -
to list all the file in the folder
$ lsfor short list without hidden file
$ ls -afor short list with hidden file
$ ls -lafor full detail list with hidden file -
to see the content of a file
$ cat file.txt -
to make your git command look beautiful you can download
posh-gitfor window -
to remove the folder for tracking on the project use
$ rm -rf .git -
Ignoring Directory or File
Before commit we need to decide which file to be send to the repositories and which wont
a. Create a gitignore file via$ touch .gitignoreor
$ echo logs/ > .gitignorethis will create a .gitignore file with logs/ content
- usecode .gitignoreto open the .gitignore in our editor and add the directory and file to be ignored example: logs/ *.log
b. If you add and commit the directory before adding it to the .gitignore its wouldn't ignore it- to solve this problem we need to remove this directory from the staging area
- let list out all the files in the staging area using
$ git ls-files
$ git rm --cached -r <files> or <directory>this will only remove the directory or file from the staging are only not working dir
- let check at
https://github.com/github/gitignoreto check the gitignore file for difference programming language.
c. you can get the template of the .gitignore at
https://www.codegrepper.com/code-examples/csharp/asp.net+core+.gitignore
- to solve this problem we need to remove this directory from the staging area
-
Add the file or folder that do not wish to be send to the repositories in the .gitignore file
*.pycthis will ignore all the .pyc extension file -
Add file to the directory or project
-
$ echo "some content here" > file1.txtor -
$ touch file1.txtwithout adding any content to it
-
-
To add more content to the file we use
>>in the echo
$ echo "new line of content" >>file1.txt -
There are three area
a. Working Directory - Stage Fixes
b. Staging Area - Commit
c. .git directory (Repository) - Checkout the project -
To View the status
a.$ git status//for full status
b.$ git status -s//for short status -
To add your file to the Staging Area use
add
a.$ git add <file>to add individual file one by one
b.$ git add -Ato add all the file at one time
c.$ git add file.js file2.jsto staging multiple files
d.$ git add .to staging the current directory and all its content
e.$ git add *.jsto staging with a pattern -
To remove the file from the Staging Area use
reset
a. use$ git reset <file>to reset the particular file
b. use$ git resetto reset all files from the Staging Area
c. or use$ rm <file>only remove from the origin not at staging area
d. use$ git rm <file>can remove the file from the origin and staging area -
Next Commit to .git directory = Now we have a snapshot in the staging area ready to be permanently stored in our repository
a.$ git commit -m "some message here"
b. you can use$ git logto see the commit summary information
c. If short one-linear description is not sufficient
you want to explain some details to give context for example if you worked on a bug
In situations like this we drop the message and just type$ git commitand press enter this opens our default editor.
A short description should be less than 80 characters then we add a line break
And follow by Long description -
you can use
$ git diffto see the change on the code you had done -
you can skip the staging step as well only if you know what you are doing if you are 100% sure that your code
your change don't need to be reviewed because that's the whole point of having a staging area
$ git commit -a -m "message here"you are skipping$ git add -aor
$ git commit -am "message here" -
move and rename the file
$ mv file1.txt main.jsthe file1.js was renamed to main.js
next we need to staging the change
$ git add file1.txtand$ git add main.js- if we use
$ git mv file1.txt main.jswe don't need to run$ git add file1.txtand$ git add main.jsagain
- if we use