Git commands # 02

01. Create and check username and email

👉 To create a username and email globally, write the following code…

For username ---

git config --global user.name ”mozahedul” → press enter 

For email ---

git config --global user.email “mozahed001@gmail.com” → press enter



👉 To create a username and email locally, write the following code…

For username → 

git config user.name ”mozahedul” → press enter 

For email → 

git config user.email “mozahed001@gmail.com” → press enter



👉 To check which email or username already exists in the Git …

git config --list → press enter



👉 Show the file and folder list in the working directory of Git …

git ls-files 

Hints: We can also use a flag like -s



##################################################


02. Staging or tracking

👉 To check unstaged or untracked and tracked or staged files…

git status → press enter


👉 How to stage or track a file  …

git add test.txt → press enter

Here → test.txt is a file


👉 How to add all files with one line of code  …

git add --all → press enter 

Or use the codes below …

git add -A → press enter

Or use the codes below ...

git add . → press enter


👉 How to commit a staged or tracked file…

git commit -m “test.txt file added” → press enter 


Here → the text inside the quotation mark is a text message.


👉 How to commit or staged together …

git commit -am “commit message”


👉 Transform tracked / staged to unstaged / untracked (undo change):

To unstage, use

git reset HEAD

Or 

git reset HEAD <file>...

Or 

git restore --staged <file name>



###############################################


03. Send file or folder or code from work directory to remote repository

Step 1: 

Add file or folder from work directory to staging area/index …

git add . or git add all → press enter


Step 2:

Add a file from the staging area to a local repository …

git commit -m “add some commit” → press enter 


Step 3: 

Add a file from the local repository to the remote repository …

git push -u origin master



###############################################


04. Commit

👉 Git command used to view your commit history

git log → press enter 


👉 To show what changes you have done in one line / see the commit in one line with commit hash ...

git log --oneline → press enter


👉 See the commit in one line with the commit hash for a specific branch from any branch...

git log <branch name> --oneline → press enter


👉 How to return to the previous session or commit ...

git log --oneline or git  log --pretty=oneline→ press enter 

Copy the ID number of the previous session or commit and then write the code below …

git checkout 21546 → press enter 

Here → 21546 is an ID number of the previous session or commit.


👉 Filter the commits:

git log --grep apples --oneline


Hints: here, commits will show which contain apples as commit text.


If we want to show the removed and changed commit, then we can type …

git log -G apples --patch


Hint: apples is a text.


👉 Show commit hash, branch(s), commit message

git log --graph --decorate --oneline

or 

git log --graph --decorate --pretty=oneline


👉 Show all commits with details

git log --patch


👉 Show specific commit details ...

git show <commit hash>

or,

git show <branch name>


👉 Show current commit details ...

git show HEAD


👉 Show commit details with the help of the index number …

With the tilde(~) sign














Or, 

With caret(^) sign: 



👉 How do I delete unpushed git commits?

Delete the most recent commit from the local repository. It doesn’t touch the staging area/index and working directory.

git reset --soft HEAD~1

git reset --soft HEAD^


👉 Delete or remove the commit with the commit hash

git reset --soft <commit hash>

Hint: don’t take the latest commit hash to remove. This command will remove the commit from the latest commit to the specific commit.


👉 Delete or remove the most recent commit, remove it from the staging area, and working directory:

git reset --hard HEAD~1

or, 

git reset --hard HEAD^


👉 Delete or remove commit, remove from the staging area, and working directory with commit hash

git reset --hard <commit hash>

Hint: don’t take the latest commit hash to remove. This command will remove the commit from the latest commit to the specific commit.


👉 Delete or remove the most recent commit, and move to the staging area/index

git reset --mixed HEAD <file name>

or,

git reset HEAD <file name>


How to delete or remove remote (GitHub) git commits from the local git repository?

Firstly delete from the local repository:

git reset --hard HEAD~1

Or,

git reset --hard HEAD

 

Secondly, delete from the remote repository:

git push --force



############################################


05. Git branch

👉 Create a branch

Write the following code in the Git bash …

git branch <branch name>→ press enter 


👉 Create a branch from a remote branch

git checkout -b whats-new origin/whats-new


👉 Check or show a branch

git branch or git branch -a → press enter


👉 Switch from master branch to any feature branch

git checkout  <branch-name> → press enter 

or, 

git switch <branch name> → press enter 

Hint: for using a switch, the git version must be > 2.30


👉 Create a branch and switch to that branch automatically

git checkout -b <branch name> → press enter


👉 Switch from branch to master / main branch

git checkout main → press enter 


👉 Merge the master branch and feature branch : 

Fast-forward merge

Before typing this command, we have to switch to the master branch.

git merge <feature-branch>


💨 Before we commit anything to master, we have to pull down the changes…

git pull origin master


💨 If we want to get the files or information from branch to master branch…

git merge <feature-branch> → press enter


💨 After that use the following command to push the changes to the master in the remote repository

git push origin master


👉 If the merge conflict occurs, then we can abort or undo the merge

git merge --abort


👉 Show merged branch

git branch --merged


After merging, we can delete or remove the branch if we don’t work with the branch anymore.

👉 Delete/remove a branch from the local repository...

git branch -d <branchname>


But If you have a branch that has commits or you wanna delete before you merge those into the branch, you will get an error message.


👉 To force delete /remove a branch from a local repository...

git branch -D <branchname>


Again we also have to be aware that we don’t remove a branch if we locate it inside this branch. 

We have to shift to the master or the main branch.


👉 Delete / remove a branch from the remote repository

git push origin --delete <branch name> → press enter


👉 Deleting/removing the references to the branches that don’t exist on the remote repository

git remote update origin --prune


👉 Rename a git branch in the local repository...

git branch -m <old branch> <new branch>

git branch -M <old branch> <new branch>


👉 Rename the current git branch...

git branch -M <new name>


👉 Shortcut for renaming a branch in the local repository…

For this, firstly we have to shift to the branch which we want to rename …

git branch -m <newbranch>


👉 How to check the difference between two branches...

git diff <branch1> <branch2>→ press enter


👉 How to check the current git branch...

git rev-parse --abbrev-ref HEAD


👉 How to send or push local git branch to remote (GitHub) git branch...

git push -u origin <local branch name>



############################################


06. Git clone from Github

👉 Git clone with the same name

Open a repository in Github→ click on Clone or download (green color button) → copy the link


Go to the folder where you want to clone the repository in the local machine→ open the Git bash or terminal → write the following code … 

git clone <remote repository link> → press enter…


👉 Git clone with the different name

write the following code … 

git clone <remote repository link> <different name>→   press enter…



############################################


07. git diff

👉 How to check the difference in the working directory between two changes…

git diff → press enter


👉 How to check the difference in staging area between two-staged change

git diff --staged

Or, 

git diff --cached


👉 How to check the difference in the local repository between two commits

git diff HEAD

or, 

git diff <old commit hash> <new commit hash>

or,

git diff <old commit hash>


👉 To get the commit hash to use the command like…

git log --oneline or git log --pretty=oneline 


👉 How to check the difference between two branches after commit

Firstly we have to move to a custom branch.

git diff <custom branch> <master or main branch>

or 

git diff <custom branch>...<master or main branch>



#############################################


08. Git fetch and git pull

👉 git fetch

With git fetch, we can import information from Github into the local repository or the local machine.

git fetch


git-fetch - Download objects and refs from another repository but never integrate them into the local repository.


👉 git pull

With git pull, we can update and show data in the working directory.

git pull


In the simplest terms, git pull does a git fetch followed by a git merge.



#################################################


09. Remove or delete

👉 Delete or remove a file from the working area, then write the codes below ...

rm <file name>


👉 If we want to delete or remove a file, then write the codes below ...

git rm remove the file from both working area and staging area

git rm test.txt → press enter

Here → test.txt is a file 


👉 If we want to delete a file, but it creates a problem then ...

git rm -f test.txt → press enter


👉 How to delete a file from a staging area or delete it permanently...

git reset HEAD test.txt

Or 

git reset test.txt

Here → test.txt is a file

Remove everything from the staging area:

git reset


👉 Remove a git repository...

rm -rf .git


👉 Delete all untracked files or unstaged files from a git repository (working area)

git clean -f 

If we want to delete untracked files by our own choice, then type the following code…


👉 Delete a specific untracked file or unstaged file

git clean -f -n

This command will show the list of untracked files. 

Then type the following command… 

git clean -f → filename → press enter


👉 Remove or delete a file from the staging area (convert tracked or staged to untracked or unstaged)

This command removes the file from the staging area but never removes the file from the working directory.

git rm --cached <filename>


👉 Transform tracked / staged to unstaged / untracked (undo changed):

git reset HEAD

Or,

git reset HEAD~

Or 

git reset HEAD <file>...



############################################


10. Git stash

👉 Create a git stash

Store some code in different places for later use.. is a temporary folder. We can use those codes when we want.

Open a file and add some code → save the file → open Git bash → write the code…

Use git stash before git add .

git stash → press enter 


Now open the file and see that no code is there.


👉 Remove/delete git stash

It removes the topmost stash from the git list

git stash pop  → press enter 


👉 How to remove a particular stash

git stash list → press enter 

git stash pop <serial of the stash (like stash@{0}) > or <use only index numbers like 0> → press enter → now open the file to show the changes.


Example: 

git stash pop o 

or 

git stash pop @


👉 Clear the entire stash

git stash clear


👉 Move stash data to the working area and index/staging area

git stash apply  → press enter 


👉 How to see the stash list

git stash list → press enter 


👉 How to see stash status

git stash status → press enter 


👉 How to stash untracked files(with .gitignore files):

To stash your working directory including untracked files (especially those that are in the .gitignore) then you probably want to use this cmd:

git stash --include-untracked

(Alternatively, you can use the shorthand -u instead of --include-untracked)



#############################################


11. target, source, & merge




target

target is the branch that we want to modify. 


source

The source is the branch that has the resource of modification.

When we merge the target branch and source branch, the target branch will change but the source will remain unchanged.

Here, the source branch will push the resources to the master or main branch but it will remain unchanged itself.


merge

To merge a branch, we have to live in the target branch. Then we have to type…


git merge <source branch>


But if we want to get the latest commit from the source branch, then we have to type…

git merge --squash <source branch>

git commit -m “commit message”



################################################


12. git reset



git reset --hard <filename> or <commit hash> ⇒ /move the data into both working area and index.



If we type the command git reset, then by default it will work the same as git reset --mixed.



##############################################


13. Renaming file

👉 File renaming in working area

mv <old file> <new file>

Example ⇒ mv page.txt page.md


##########################################


14. cherry-pick

With cherry-pick, we can pick a commit from a branch and apply it to another branch.


Syntax

Firstly we have to find or show the list of commits of the source branch. Then we have to copy the commit hash that we want to pick and apply it to another branch.


Show the commit list…

git log --pretty=oneline 


Then move to the target branch and type the following command 

git cherry-pick <commit hash> 


#################################################


15. .gitignore

👉 Ignore a git ...

touch .gitignore

N.B → opens the .gitignore file and pastes the files name you want to ignore.


It’s a good idea to put the .gitignore file in the root folder. 

*  ? ! / [a-z A-Z]     ⇒ anything, one character, negator, directory separator, range.


# this is a comment ⇒ # for comment


**/bin  ⇒ ** matches any directory in the repository


*.zip  ⇒ * matches any zip file in the repository 


/bin ⇒ relative to .gitignore directory.



##############################################


16. git rebase

👉 git rebase

git rebase clean up local history and focus on end results.


Should increase accuracy and clarity. 

Not mandatory.

Do not use rebase on a public branch.


We can squash commits into one commit.

















👉 Remove any of the commits …

git rebase -i origin/main → press enter 

Many options will show, and select any of the commands and add them at the left side of the commit.


Here, -i is the shorthand of --interactive


17. git blame

👉 Show the commit list in a file...

git blame <file name>


18. Modify the latest commit message:

👉 Modify the latest commit message...

git commit --amend -m “commit message”


18. git reflog:

👉 This command is used to restore the previous state...

git reflog


👉 Branch-specific reflog...

git reflog refs/heads/main


👉 Need Help ...

git config help

Or,

git help config


👉 Viewing information about the remote repository ...

git remote -v

Or,

git branch -v


👉 If we want to see the content inside the file.

cat <file name>


Post a Comment

Previous Post Next Post