Hi friends,
Today I would like to share you a list of Git commands that I consider really useful when we work with this great version control system. Enjoy it!
Configuring your Git
// Disable autocrlf to avoid warnings in command line git config --global core.autocrlf false
// Set username and email (global) git config --global user.name “My user name” git config --global user.email “my-email@awesome.com” // Display configuration git config -l // Get some help :) git contig -h
Managing Git repositories
Initializing a repo
git init
Adding files for tracking or to the staging area
// Add all .txt files git add *.txt // Add all in the current directory git add . // Add a single file git add README.md
Resetting your files in the staging area to the working directory
git reset HEAD <file> git reset HEAD -- <path/to/file> // Examples git reset HEAD index.html git reset HEAD .
Doing your changes persistent in the local repository
// Do a commit with all files in staging area git commit -m "My custom message for this commit"
Watching previos version of files
// Watch a version of a very specific file (using the commit ID) git checkout YOUR_COMMIT_ID -- path/my_file.js // Return to the version of that file in HEAD git checkout HEAD -- path/my_file.js
Watching logs
// Watch the log (details) git log // Watch the log in one line per commit git log --oneline // See two last commits git log --oneline -2 // Show diff between files git show
Merging a branch (my-branch) into other (master)
// Where do you want to merge? git checkout master // Which branch do you want to merge? git merge my-branch
Restoring files
See the stored version of an specific file in a commit:
git checkout COMMIT_ID "path/to/the/file" // Example git checkout 6881326 "widgets/Awesome Widget/MyAwesomeWidget.dust.tpl.htm"
Working with branches
Creating, deleting and switching to a branch
// Get a list of branches in the repository git branch // Create a new branch git branch my-branch // Switch to a branch git checkout my-branch // A shorthand for two previous commands (create and switch) git checkout -b other-branch // Delete a branch git branch -d my-branch
Working with submodules
A very nice feature available on git is the possibility of using modules, for example when you clone CakePHP repository you could get DebugKit as a submodule, if you want to clone the main repository + submodules, use following command:
git clone --recursive https://github.com/alex-arriaga/universidad-del-pastel.git
Adding a new submodule
git submodule add <GIT_REPO_URL> <SUBMODULE_NAME> // Example git submodule add https://github.com/twbs/bootstrap.git bootstrap
If someone else added a new submodule, and you want to get submodules, you will need to use the update operation to obtain the module recently added:
git submodule update --init --recursive
Changing a submodule for another one
- Update .gitmodules with the path to the new repository
- Remove the corresponding line from the “.git/config” file
- Delete the corresponding directory in the “.git/modules/modules/YOUR_OLD_SUBMODULE” directory (rm -rf)
- Delete the checked out submodule directory itself (unsure if this is necessary)
- Run:
git submodule init and git submodule update
Working with remotes
Forcing a push in origin (remote) to a previous version as in our local repository
// Suppose you want to return to a previous version // in your local repository git reset --hard YOUR_COMMIT_ID // Then, probably you will need to force the remote to stay // in the same as your local repository git push origin master --force // Or git push origin master -f
Sometimes if the remote server doesn’t have well-configured SSL certificates, you could get an error. If you want to disable SSL verification which I don’t recommend, use next command:
// Disable the SSL CERT verify globally git config --global http.sslVerify false // Disable the SSL CERT verify only in current repository git config http.sslVerify false
Changing the URL of a remote:
git remote set-url <REPOSITORY_NAME> <NEW_URL> // Example git remote set-url origin git://new.url.here
Deleting remote branches
// List all the branches (local and remote) git branch -a // Delete a branch git push origin --delete MY_BRANCH
One comment on “Some useful Git commands”