Skip to content

DharmendraRathor/gitCommands

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 

Repository files navigation

Important Git Commands 

Commands Description
git init

Create git repository on locale machine.

git Status

View list of files modified.

git clone username@host:/path/to/repository

 

Checkout exiting git repository

git checkout <branchName>

Switch to existing branch

git checkout -b <branchName>

Create new branch and switch to that branch

git add <file name with path>

Add single file to staging

git add *

Add all file to staging
git stash list

List of stash done earlier.

git stash apply "stashId" eg git stash apply stash@{0}

To apply stash .

git commit -m "commit message "

Commit files in staging // this is committed to local repository and not to remote

git push

Publish changes to remote repository

git pull

Pull changes from remote branch to your branch (this will fetch and merge changes)

git branch 

List all branches on local respository and current active branch name as highlited

git log
git log -2

View commit history

git merge <branchTobeMerged>

Merge branch with your local branch

git diff

View changes done before pushing it to remote.

 git remote show <branchName>

eg git remote show origin

View git remote url or remote branch details. 
git checkout -b <branchName> <tagName>  Create branch from Tag in Github
git cherry-pick <commit-id>
eg git cherry-pick ...31ff5e.....

 Get Cherry picking commit

if you have to cherry pick commit from one branch to another in Git repository with Commit id.

git branch newBranchName <commit-id> 

Create new Git branch from Git commit

Create new branch from git commit withouth checkingout new branch

git checkout -b newBranchName <commit-id>

Create new branch from git commit with checkingout new branch

 

A Moving branches/tags from one git repository to another

1. Clone existing git repo (source repository)
git clone --mirror <source git url>

eg
git clone --mirror git@github.com:source/source.git

2.
got to <folder with name of repo>
cd <folder with name of repo>

3. Add remote repository ( destination repository )
git remote add new-origin <destination git url>

eg
git clone --mirror git@github.com:destination/destination.git

4. Move all changes to destination repository
git push new-origin --mirror

 

B If you only want to move some branches


1. git clone <source git repository url>

2. checkout all branches to be moved to new git.
git checkout <branch>

3. verify branches to be moved using output of git branch command
git branch

4. add destination repository
git remote add new-origin git@github.com:destination/destination.git

5. move all local branches to destination repository
git push --all new-origin

6. move all tags to destination repository
git push --tags new-origin


C Move branch to new git repository and changing remote

after doing steps mentioned above to move branch from one git to another , use following command to set new
origin in git.

git remote rm origin
git remote rename new-origin origin



Note :
If you have already moved some branches and want to move one more branch from source git.
Running following command might override destination branche or give error.
It is always good to move all branches and tags to new repository and start using new repository.

 

Moving branches/tags from one git repository to another

 

You have branch having latest changes “latestBranch” and if you want to override master with latestBranch changes.
Use following commands

1. Checkout latestBranch.
git checkout latestBranch

2. Merge latestBranch with master with strategy as ours. Current branch changes will be used in case of conflicts.
git merge -s ours master

3. Checkout master branch.
git checkout master

4. Merge master with latestBranch
git merge latestBranch

5. Push master with latest code.
git push (git push origin master)

you can also use git force push “git push -f origin master”. Please check branch history before running git push

Replace/override Master branch with your latest branch in git

Revert merge request 

Find commit id of merge request , it can also be seen from UI 

or using grep and git log command eg 

git log -200 | grep -B 20 "Merge pull request #12"

 

Once you have commit id , run following command 

 

git revert -m 1 <commitId>

 

In case of conflict resolve them manually and add files using "git add "

 

(fix conflicts and run "git revert --continue")

 

git push

Revert direct commit with commit Id 

git rever <commitID>

Added file by mistake using "git add fileName.txt" and want to remove it before commiting from index  

git reset fileName.txt

Revert "git add ." from index  

git reset

Set Git config username and email  

git config --global user.name "firstName lastname"

git config --global user.email "abc@test.com"

Create new git repository 

git clone git@git.com:platform/project.git
cd "projectFolder"
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master


Link existing code to Git repo 

cd existing_folder
git init
git remote add origin git@git.com:platform/project.git
git add .
git commit -m "Initial commit"
git push -u origin master

Link existing git repo to new git repoo 

cd existing_repo
git remote rename origin old-origin
git remote add origin git@git.com:platform/project.git
git push -u origin --all
git push -u origin --tags

 

About

Project contains important git commands

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published