Tuesday, December 10, 2013

Common git commands

Common basic git command

git init:
To create a new local git repository to run the git command

git branch new-branch-name
To create a new branch
This will use the current workspace's HEAD to create a branch. This command will create the new branch in repository, but it will not switch the current workspace to the new branch; use "git checkout <newbranch>" to switch to the new branch.

git checkout branch
switch the current workspace to the specified branch.
To combine the operation of create a branch and checkout it in a single command, use the following command:
"git checkout -b NewBranchName referenced-headername"
If the current branch has local added but uncommitted change, then you cannot switch to another branch by using git checkout. you need first commit or reset the change in current branch.

git branch
git branch -a
Execute "git branch" will show all local branches available in the repository, the current head branch is started with *.
"git branch -a" will list all remote and local branches

git add:
Add local change
take a stage snapshot of the local workspace change for future commit, it just creates snapshot for the current change in the workspace, no new head will be added in git repository. You can use git add multiple times and then commit them altogether

git commit
git commit --amend
To commit local change to repository
Git commit only commit the change from the snapshot created by git add.
--amend will add then new staged change into the previous commit. Running this when there is nothing staged lets you edit the previous commit’s message without altering its snapshot. After the commit message is changed in vi, for windows, press esc key and then type ":wq" to save the change and exit the editor.

git branch -d BranchName
delete a branch in local repository

git rebase SomeBranch
To apply the change in SomeBranch to the current branch from their common parent base head.

git pull
To pull server change without local committed change

git pull --rebase
integrate remote change to local branch

git fetch origin remoteBranchName
git checkout remoteBranchName
fetch a remote branch content to local and then checkout the branch. This command can be used when someone sends you a branch which contains some temporary change.

git push origin master
To push the local committed change to server branch

git clean -f -d
Remove local untracked file and folder

git checkout .
Revert all local change in existing files that are not staged with "git add" command.

git reset
revert the staged change made with "git add" command. the local change in workspace is still kept as local, unstaged change.  (git reset FilePath can also be used for single file )

git reset --hard
remove the staged change created by git add, and revert the local change back to the most recent commit. The local change will be lost

git revert PreviousCommitHash
make a commit to revert the previous change specified in previousCommitHash

git reset HEAD^
remove the last commit but keep the change in local

git add -u
Add all deleted file into added change for commit.

git cherry-pick head1
merge the change from one branch to another
1. get the sha1 number for the change
2. goto the dest branch and update the code to latest
3. run git cherry-pick sha1
4. commit the change to the new branch

git stash
save the current change with default name

git stash save "name"
save a named change to stash

git stash list
show the existing stash

git stash pop
git stash pop stash@{0}
pop up a saved stash

git stash drop
delete the top stash without using it.

git stash apply
apply the last saved stash change, but do not delete it

Common comparison commands

git diff
compare local workspace change with added change (or committed change if no staged change available)

git difftool --cached
compare the difference between the added snapshot (before commit) and the last committed change

git difftool HEAD^ HEAD
compare the difference between the last commit and the previous commit

git diff HEAD
compare local workspace un-added change with the last committed change

git diff head1
compare the specified commit (head1) with the last commit (HEAD)

git diff commit1 commit2
compare all the changes made after commit1 (commit1's change is excluded), but including the changes made in commit2

git diff head1 head2
compare the specified commit head1 with head2

git diff head1^ head1
compare the change made by commit head1 with its parent

gitk -all &
show the visual commit history for all branches without block the terminal command

Git Tag for version management

git tag -a yourReleaseVersionNumber -m "release comment"
after a version is released, a git tag should be created to stamp the code that is associated with this release

git tag
show the existing tags

git show yourReleaseVersionNumber
show the commit message for the tag

git tag -d yourReleaseVersionNumber
delete an existing tag

git push origin yourReleaseVersionNumber
push the tag to remove repository

git checkout yourReleaseVersionNumber
checkout an existing tag. After checkout a tag (v1.0) as detached header, you need to use checkout -b to create a new branch (v1.1) based on the tagged code base, and then check in the change into the new branch (v1.1)


Update forked repository

assume you already in your local master branch, then follow the below steps

1. add remote (original) repo and call it upstream
git remote add upstream https://github.com/manishjanky/ngx-select-dropdown.git

2. fetch the change from remote upstream
git fetch upsteam

3. update local master to remote upstream's master
git rebase upstream/master

4. push your local master change to your remote forked master
git push origin master --force

1 comment:

  1. Good blog. Keep sharing. I love them Are you also searching for Cheap assignment help? we are the best solution for you. We are best known for delivering cheap assignments to students without having to break the bank

    ReplyDelete