Basic Git Command Line on Terminal

Following are some useful command lines for Git operations, running on Mac’s Terminal.

Display Git version.

$ git --version

Initialize Git in a folder. This tells Git that you want to use your the current folder you are in as Git repo

$ git init

Show pending changes (files you have changed and haven’t committed)

$ git status

List all branches

$ git branch

Switch to ‘master’ branch

$ git checkout master

Add remote source named ‘origin’

$ git remote add origin https://xxxx@bitbucket.org/xxxx/xxxx.git

Download existing branch (‘beta’) from ‘origin’ and save it as ‘beta-test’ branch in local. Local branch can have the same name as the remote branch, this is just to show parameter order.
After fetching from remote, the new branch will NOT have ‘git pull’ tracking configured for that branch. Call ‘branch’ command with ‘–set-upstream-to’ to set tracking information.

git fetch origin beta:beta-test
git checkout beta-test

Set tracking information for local branch called ‘master’ to ‘origin/master’. Must already have the ‘origin/master’ in remote branch.

$ git branch --set-upstream-to=origin/master master

Show remote sources

$ git remote show

Show ‘origin’ info remote source

$ git remote show origin

Look up current configured remote source

$ git remote -v

Pull latest version from ‘origin’ source control after setting remote source

$ git pull origin master

Pull latest version from source control without setting remote source

$ git pull https://xxxx@bitbucket.org/xxxx/xxxx.git

Add a new file to Git repo

$ git add something.txt

Add all untracked files to Git repo

$ git add .

Remove whole folder and all its content from index (untrack a folder and all its content)

$ git rm foldername -r

Commit to repo with ‘Hello World’ message

$ git commit -m 'Hello World'

Push your commits to the ‘master’ branch of ‘origin’ source to Git

$ git push origin master

Create a local clone of a repository

$ git clone https://xxxx@bitbucket.org/xxxx/xxxx.git

Fetch from ‘origin’ source

$ git fetch origin

Merge ‘master’ branch of ‘origin’ source to local copy

$ git merge origin/master

Push and track ‘branch1’ to remote ‘origin’. This is for branch that doesn’t yet exist in remote.

$ git push -u origin branch1

Undo (discard) all uncommitted changes.

$ git reset --hard

Undo (discard) all committed (and uncommitted) changes that haven’t been pushed.

$ git reset --hard HEAD~1

To delete a local branch

git branch -d the_local_branch

To remove a remote branch (if you know what you are doing!)

git push origin :the_remote_branch

Create a link (remote) from one repo to another. Git ‘pull’ here is used to download the repo’s branches and tracking information.
See here for more details.

git remote add repo-A-branch <git repository A directory>
git pull repo-A-branch

Cherry pick commit with id 86f4c61 to current branch

$ git cherry-pick 86f4c61

Git Configurations

$ git config

Save git credentials

$ git config credential.helper store
// Can be any command that requires credentials.
$ git push http://example.com/repo.git
Username: <type your username>
Password: <type your password>

// Now the credentials are stored and will be used for any command need credentials
$ git push http://example.com/repo.git
Level Location Description Command Option
System /etc/gitconfig System level configuration
C:\Program Files\Git
–system
Global .gitconfig
OR
/.config/git/config
Global level configuration
C:\Users\
–global
Local /.git/config Repository level configuration
Local repository folder

Fork vs Clone
Clone doesn’t let you contribute back to the ‘upstream’ repository unless you are explicitly declared as “contributor”. Fork allows you to request a pull from ‘upstream’ repository

Pull vs Fetch
git pull does a git fetch followed by a git merge

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s