Useful Git Commands

CacteGra
3 min readNov 7, 2019

Add a remote repository, origin is the name to use next time you push your changes to the remote repository (can be another folder or a URL:

git remote add origin address_to_your_remote_git.git

Change remote repository URL/:

git remote set-url origin remote.git.url/path/to/remote/repository

Add all current files:

git add .

Add only selected file:

git add name_of_file

See changes that will be committed:

git diff

Change past commit text:

git rebase -i (commit hash (example: szpfdfpy2qbj36qb7xwevtgw5fm8trc8bkupjryx7))

Will give you:

change “pick” to “reword”

A file will be opened to change the commit text.
Push the changes to the text:

git push origin --force

error: failed to push some refs to ‘https://git.heroku.com/digitalquint.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: ‘git pull …’) before pushing again.

Unstaged local changes (before you commit):
Discard all local changes, but save them for possible re-use later.

git stash.

Discarding local changes (permanently) to a file.

git checkout — <file>

Discard all local changes to all files permanently.

git reset — hard.

Remove last commit and revert its changes:

git reset — hard HEAD~1

See past commits, returns hash key (a series of letters and numbers), author, date and time, and comment for each commits:

git log

See past commits for a particular file:

git log -- file_name

See file differences for each past commits for a particular file:

git log -p -- file_name

Show file state at a particular commit:

git show hash_key:file_name

Example:

git show 1606t5ab2e453a5bee96f506b5fc6111793a7b56:file_name

Revert to hash commit:

git revert hash_key

Change the description of your most recent commit:

git commit --amend

(If on GitHub, create a project there first using the name of the local parent folder, where git initialised)

List local branches:

git branch

List remote/local branches:

git branch -a

Delete branch:

git branch -d name_of_branch

Make first push to remote git, will ask for a username and password:

git push -u origin master

Push changes:

git push origin master

If an error occurs of the type:

error: failed to push some refs to ‘address_to_your_remote_git.git’
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: ‘git pull …’) before pushing again.
hint: See the ‘Note about fast-forwards’ in ‘git push — help’ for details.

If you are sure remote changes are in fact not behind but include all changes, simply run:

git push -f origin master

Get the commits from remote:

git fetch origin

Then check the differences between local and remote with:

git diff HEAD..origin/master

to rebuild from local:

git pull — rebase

to rebuild from remote:

git pull — merge

Add another repo to your project:

git submodule add remote_path_to_the_other_repo 

Push changes to the submodule:

git checkout master
git commit -a -m “the changes”

Push your commit to the remote repository:

git push

Change directory to project root:

git add path/to/submodule_directory
git commit -m “the changes”
git push

Clone repository with submodules:

git clone — recurse-submodules name_of_system_user@server_address:/path/to/directory

Delete files inside a git repository:

git rm the_file
git rm -r the_folder
git rm the_* ## removes any file beginning with the_ ##

Detached HEAD issue:
If you want to delete your changes associated with the detached HEAD

You only need to checkout the branch you were on, e.g.

git checkout master

Next time you have changed a file and want to restore it to the state it is in the index, don’t delete the file first, just do

git checkout — path/to/foo

This will restore the file foo to the state it is in the index.

If you want to keep your changes associated with the detached HEAD

Run git log -n 1; this will display the most recent commit on the detached HEAD. Copy-and-paste the commit hash.
Run

git checkout master

Run

git branch tmp <commit-hash>

This will save your changes in a new branch called tmp.
If you would like to incorporate the changes you made into master, run git merge tmp from the master branch. You should be on the master branch after running git checkout master.

--

--

CacteGra

It's all about interweaving words and code to create something new.