Git command-line interface (CLI)

In a nutshell, git commits are a tree, and branches are just pointers to some commits.

git clone [repository clone URL]

git checkout [name of branch]
git checkout master
- OR -
# force to proceed even if the index or the working tree differs from HEAD. This is used to throw away local changes. or use git reset --hard
git checkout -f master

#
git pull  //is shorthand for git fetch followed by git merge FETCH_HEAD
git pull --rebase  //it runs git rebase instead of git merge. Rebasing Deletes Merge Commits!
git fetch //it doesn’t touch your working tree at all, downloads the latest from remote without trying to merge or rebase anything
git fetch --all  

git remote update //will update all of your branches set to track remote ones, but not merge any changes in.
git fetch //only current branch 
git pull //only current branch 
get remote update origin // same as git fetch origin
# display the state of the working directory and staging area (eg: staged files, untracked files)
git status

# display the entire commit history
git log
# add files to the Git index (staging area) but not committed 
git add [a filename OR space separated list of filenames]

git add -A //stages All
git add . //stages new and modified, without deleted
git add -u //stages modified and deleted, without new

# commit the files in the index to your local repository, with a message
git commit -m "[message goes here]"

# Or, can combine both actions
git commit -am "[jira-id] [commit message goes here]"

# push your changes to the remote repository
git push

Git has a number of ways to undo changes -

# undo changes to a file(s) in your workspace
git checkout -- [file OR space separated list of filenames]
git checkout -- myfile  //Undo modifications (restore files from latest commited version)
git checkout 6eb715d -- myfile   //Restore file from a custom commit in current branch

# undo all your changes that have NOT been committed (get rid of all local changes)
git reset --hard  // same as git reset --hard HEAD

# undo changes that have been shared with others. This particular command makes a new commit that reverts back two commits
git revert HEAD~2

Remote

If you clone a repository, the command automatically adds that remote repository under the name origin. And by default, the git clone command automatically sets up your local master branch to track the remote master branch (or whatever the default branch is called) on the server you cloned from.

# git push <remote> <branch>
git push origin master

note:

# You should have done this first
git branch --set-upstream-to=origin/master  // then do git pull
git pull
# then
git push  <REMOTE_NAME>  <LOCALBRANCH_NAME>:<REMOTEBRANCH_NAME> 
git push origin master  // master maybe ok
git push origin solaris  // this one may have issue
# otherwise
git push -u origin localBranch:remoteBranchToBeCreated
git push -u origin HEAD  //it is a "handy way to push the current branch to the same name on the remote". HEAD (in uppercase) is a reference to the top of the current branch (tree).

# issue
git push origin solaris
** that creates solaris on origin, and hence also creates origin/solaris in your own Git repository. 
** But it's too late: you already have a local solaris that has no upstream.
** The fact is,  you should have used --set-upstream or -u during the git push. 
** To set it now, rather than during the first push, use 
git branch --set-upstream-to
git push -u // or --set-upstream, add upstream (tracking) reference,
# inspecting a Remote
git remote show origin

git checkout <branch>   // go to the branch you want to check difference
git diff origin/<branch>   // now you can check difference
#-OR-  
git diff @ @{upstream} 
// The first @ is HEAD, which is where you are now, so you are comparing HEAD with the upstream your branch is tracking. 
// The suffix  @{upstream} to a branchname (short form <branchname>@{u}). 
// A missing branchname defaults to the current one.
git diff <local branch> <remote>/<remote branch>
git diff master origin/master 
git diff featureA origin/next

# if you just compare with your upstream branch.
git diff origin

results matching ""

    No results matching ""