0% found this document useful (0 votes)
104 views6 pages

Essential Git Commands Guide

Git is a version control system used to track changes to files. Key Git commands include: 1) git config to set the author name and email for commits. 2) git clone to obtain a repository from a URL. 3) git init to create a new local Git repository. Git allows saving changes temporarily with git stash and applying them later. Tags can be added to commits with git tag. Branches allow independent lines of development.

Uploaded by

Aakash Mazumder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views6 pages

Essential Git Commands Guide

Git is a version control system used to track changes to files. Key Git commands include: 1) git config to set the author name and email for commits. 2) git clone to obtain a repository from a URL. 3) git init to create a new local Git repository. Git allows saving changes temporarily with git stash and applying them later. Tags can be added to commits with git tag. Branches allow independent lines of development.

Uploaded by

Aakash Mazumder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Usage: git config –global user.

name “[name]”  

Usage: git config –global user.email “[email address]”  

This command sets the author name and email address respectively to be used with your commits.

git clone

Usage: git clone [url]  

This command is used to obtain a repository from an existing URL.

git init -> The git init command creates a new blank Git repository. It is used to make an existing project as
a Git project. Several Git commands run inside the repository, but init command can be run outside of the
repository.

git log-> If there is no commit to master branch then the message will come as “ your current branch 'master'
does not have any commits yet”

Touch <<filename>> -> To create a new file through terminal (macbook)

Vi <<filename>> -> Edit the file

To save the File click esc key and then type :WQ and Enter

git status->

On branch master

No commits yet

Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: calc

git diff

Usage: git diff  

This command shows the file differences which are not yet staged.

git diff –staged 

This command shows the differences between the files in the staging area and the latest version present.

Git reset

This command unstages the file, but it preserves the file contents.

git commit -m "Changes saved"


[master (root-commit) e58c536] Chnages saved
1 file changed, 3 insertions(+)
create mode 100644 calc

git log

commit e58c5360ce9f7e866ab1145581daa65bfe1ae1f9 (HEAD -> master)


Author: Akash <[email protected]>
Date: Sat Oct 31 16:45:16 2020 +0530

Changes saved

git branch newbranch -> Will create a newbranch

git checkout newbranch -> Switched to branch 'newbranch'

say we modify the file again and saved it.

git commit -m "modified file"

Changes not staged for commit:


(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: calc

Hence we need to give first Git add <<filename>>

git add calc and then gitcommit –m “modified file”

Did some changes in calc file from newbranch

git merge newbranch master

Updating e58c536..a9e8079
error: Your local changes to the following files would be overwritten by merge:
calc
Please commit your changes or stash them before you merge.
Aborting

git add calc

git commit -m "newbranch modified"

[newbranch 583c954] newbranch modified


1 file changed, 1 insertion(+), 1 deletion(-)

git merge newbranch master

Auto-merging calc
CONFLICT (content): Merge conflict in calc
Automatic merge failed; fix conflicts and then commit the result.

git diff

index 964bac4,2c4a016..0000000
--- a/calc
+++ b/calc
@@@ -1,3 -1,3 +1,7 @@@
add a + b
sub a-b
++<<<<<<< HEAD
+div a/b
++=======
+ Multiply a*b
++>>>>>>> master

vim calc

add a + b
sub a-b
<<<<<<< HEAD
div a/b
=======
Multiply a*b
>>>>>>> master

after making the changes

switch to master by typing the following command

git checkout master

git merge newbranch master

Updating a9e8079..106f693
Fast-forward
calc | 2 ++
1 file changed, 2 insertions(+)

I came to newbranch

git stash save "changes temp saved"

No local changes to save

vim calc

git stash save "temp save"

Saved working directory and index state On newbranch: temp save

git stash list

stash@{0}: On newbranch: temp save

now lets suppose we add a new file name memo in newbranch


we add that file in staging area and committed it

we made some changes in the file and save it

Now if we give the following command

git stash save "chnages made"

This command stashes with a message

Saved working directory and index state On newbranch: chnages made

git stash list

stash@{0}: On newbranch: chnages made


stash@{1}: On newbranch: temp save

git stash show

memo | 1 +
1 file changed, 1 insertion(+)

If you want to see the full difference then type

git stash show -p

diff --git a/memo b/memo


index bbbf207..1464b70 100644
--- a/memo
+++ b/memo
@@ -1,2 +1,3 @@
Thank you Master
+Welcome to my Home
git stash apply-> This command takes the top most stash in the stack and applies it to repo

git stash pop-> This command is very similar to stash apply but it deletes the stash from the stack after it is applied.

git stash drop stash@{0}-> This command deletes the latest stash from the stack

git stash clear -> Deletes all the stashes made in the repo.

git stash show ->

It is giving now as “No stash entries found.”

 git stash: Saves all the modified files temporarily


 git stash list: Lists all the stashed sets
 git stash apply: Applies the latest stashed content
 git stash pop: Applies the latest stashed set and drops it
 git tag: Adds a tag to a commit
 git tag -a -m: Adds an annotated tag with a message
 git push tag name: Pushes tag to the remote repo

git push command push commits made on a local branch to a remote repository. The git


push command basically takes two arguments: A remote name, for example, origin. A branch
name, for example, master.

Add

Commit

Push

Merge

Add the a v1.0 tag to the commit.

Use the git tag v1.0 command to add a tag to the current commit.

Apply the latest stash to the working directory by using git stash apply stash@{0}
Push the new release version v2.0 to the remote repo by using the git push origin
v2.0 command.

Use git push origin --tags to push all the tags to the remote repository.

git log –oneline to view the commit history.

 git show HEAD displays the last changes made with the commit message Reads users
surname.

 git show HEAD~1 displays the previous changes made with the commit message Reads
users first name.

git cherry-pick c5cdf47afa671cb44539d76cd125cbcf8b43ee05

Git submodule will allow you to add a vendor library to your project and get their future updates instantly.
To add a Git submodule use:

git submodule add <URL of vendor library>

Git Submodule - Cheat Sheet


 git submodule add <URL>: Adds a submodule to the project.
 git submodule status: View status (working, staging, or indexed files) of all the submodules.
 git submodule update: Updates submodules after switching branches.
 git submodule update --init: After cloning a new repo, if you need to add submodules to it from
.gitmodules file, use this command.
 git submodule update --init --recursive: If the submodules inside a newly cloned repo are nested, then
use this.
 git submodule update --remote: Pulls all changes in the submodules.

You might also like