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 |
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) eg 2. 3. Add remote repository ( destination repository ) eg 4. Move all changes to destination repository
B If you only want to move some branches
2. checkout all branches to be moved to new git.
after doing steps mentioned above to move branch from one git to another , use following command to set new git remote rm origin
|
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. 1. Checkout latestBranch. 2. Merge latestBranch with master with strategy as ours. Current branch changes will be used in case of conflicts. 3. Checkout master branch. 4. Merge master with latestBranch 5. Push master with latest code. 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 |
Link existing code to Git repo |
cd existing_folder |
Link existing git repo to new git repoo |
cd existing_repo |