List of my most used commands and shortcuts in the terminal for Mac - 0nn0/terminal-mac-cheatsheet. For Git related commmands you can check my git-basics-cheatsheet. Finally, you push all of the changes you made in the local repository to the remote repository with the command, git push. This command pushes the latest commits from the local repository to the remote repository. More about specific git commands including a long list of more advance commands can be found in a blog post by David Fredman. Git config commands. Git config -global user.name username``` The commands above are used to set current user email and name configuration. Git config -global -edit. And this command is very useful, as it allows for editing user configuration in a text editor. Not everyone likes to use git in the command line.
There are many different ways to use Git. Git supports many command-line tools and graphical user interfaces. The Git command line is the only place where you can run all the Git commands.
The following set of commands will help you understand how to use Git via the command line.
Basic Git Commands
Here is a list of most essential Git commands that are used daily.
Let's understand each command in detail.
Git config command
This command configures the user. The Git config command is the first and necessary command used on the Git command line. This command sets the author name and email address to be used with your commits. Git config is also used in other scenarios.
Syntax
Git Init command
This command is used to create a local repository.
Syntax
The init command will initialize an empty repository. See the below screenshot.
Git clone command
This command is used to make a copy of a repository from an existing URL. If I want a local copy of my repository from GitHub, this command allows creating a local copy of that repository on your local directory from the repository URL.
Syntax
Git add command
This command is used to add one or more files to staging (Index) area.
Syntax
To add one file
To add more than one file
Git commit command
Commit command is used in two scenarios. They are as follows.
Git commit -m
This command changes the head. It records or snapshots the file permanently in the version history with a message.
Syntax
Git commit -a
This command commits any files added in the repository with git add and also commits any files you've changed since then.
Syntax
Git status command
The status command is used to display the state of the working directory and the staging area. It allows you to see which changes have been staged, which haven't, and which files aren?t being tracked by Git. It does not show you any information about the committed project history. For this, you need to use the git log. It also lists the files that you've changed and those you still need to add or commit.
Syntax
Git push Command
It is used to upload local repository content to a remote repository. Pushing is an act of transfer commits from your local repository to a remote repo. It's the complement to git fetch, but whereas fetching imports commits to local branches on comparatively pushing exports commits to remote branches. Remote branches are configured by using the git remote command. Pushing is capable of overwriting changes, and caution should be taken when pushing.
Git push command can be used as follows.
Git push origin master
This command sends the changes made on the master branch, to your remote repository.
Syntax
Git push -all
This command pushes all the branches to the server repository.
Syntax
Git pull command
Pull command is used to receive data from GitHub. It fetches and merges changes on the remote server to your working directory.
Syntax
Git Branch Command
This command lists all the branches available in the repository.
Syntax
Git Merge Command
This command is used to merge the specified branch?s history into the current branch.
Syntax
Git log Command
This command is used to check the commit history.
Syntax
By default, if no argument passed, Git log shows the most recent commits first. We can limit the number of log entries displayed by passing a number as an option, such as -3 to show only the last three entries.
Git remote Command
How To Install Git On Linux, Mac Or Windows
Git Remote command is used to connect your local repository to the remote server. This command allows you to create, view, and delete connections to other repositories. These connections are more like bookmarks rather than direct links into other repositories. This command doesn't provide real-time access to repositories.
A few swipes of your hand and it’s done for you
- Edit .bash_profile on Mac
- Windows Git Bash
- Try aliases
- Windows Doskey
This is a hands-on tutorial on how you can configure and use Terminal and macros to save time working with Git and GitHub. The steps are intended for “newbies” new to the operating system.
Windows users: skip to the Windows installation section.
Default Terminal on MacOS
To open the Terminal program that comes with MacOS:
- Click the blue Finder icon at the left side of the Launch bar at the bottom of the screen.
- Click Go on the menu or press at the same time shift + command + A keys.
- Scroll down to click the click the Utilities folder.
- PROTIP: Drag the Terminal icon and drop it on the Launchbar so it’s easier to find in the future.
- Click the Terminal icon to open it.
- Press command + N to open using the Basic (default) Terminal settings (white background).PROTIP: Alternately, enjoy different colors by clicking menu item Shell then New Window and selecting one of the options listed:
- Grass (dark green) I cd to my public website posts that go to GitHub
- Ocean (dark blue) I cd to my private notes repository
- Red Sands I cd to the code repository I’m working on
- Homebrew (green font on black) I cd to the server I’m working on
Alt Terminal on MacOS
PROTIP: Alternately, some prefer to use a 3rd-party Terminal program which has additional features.
Git Commands For Mac High Sierra
- Read about features not in the default program:To download and use it:
- Open a Terminal (as shown above).
- Install Homebrew.
- Use Homebrew to downloadbrew install -g iterm2
- Open a Finder and Go to Applications. Scroll to click iTerm2.
- PROTIP: Drag the Terminal2 icon and drop it on the Launchbar so it’s easier to find in the future.
- Read its documentation:
Edit .bash_profile on Mac
- Install a text editor you want to use.
- Open a Terminal window.
- If you have a new MacOS machine, create a file on your Home folder that MacOS executes before opening any Terminal window:
~
designates the home folder for you account.'#'>>
adds a comment (#) to the bottom of the file in case the file has already been created, rather than wiping out the file..
in front of a file (in *nix systems such as Mac) denotes a hidden file.
- In a Terminal, open to edit. Different editors have different commands. In this example, the Nano text editor is being used because Nano is built into MacOS:nano ~/.bash_profile
Git shortcuts on MacOS
With the~/.bash_profile
file in an editor: - Highlight these lines, then press command+C to copy it your machine’s (invisible) Clipboard:
Switch programs
- Press command+Tab repeatedly until you see the icon for the text editor.
- In the text editor, click your mouse at the bottom of the file.
- Press command + V to paste from the Clipboard.
- Save the file using the command expected by the editor you’re using.For Nano, press control + W.
Update Terminal
- Open a new Terminal instance, which loads the new version of bash_profile.
Try sbp alias for Mac
- Instead of typing out
source ~/.bash_profile
, type:sbpThis invokes the alias defined:This command just returns another prompt.Edit aliases
You can delete the aliases you want or add others, then save the file again. - Switch back and forth between the text editor and
- Remember to source the file or open a new window.
- Skip to create Git container.
Windows Git Bash
- Click the Windows icon at the lower-left corner of your Desktop.
- Type “Git”. If you see Git Bash, you likely used
choco install git
to install Git.Alternately, you would need to add a folder and edit the Path system environment variable. - PROTIP: Right-click on “Git Bash” and select “Pin to taskbar” so it can be accessed quickly in the future.
- Open Notepad: click the Windows search icon, type no and click on Notepad in the list that appears.Alternately, you can use another text editor (such as Visual Studio Code).
- Highlight and press Ctrl+C to copy the following to your Clipboard.TODO: Figure out a replacement for this:
- Click Notepad menu File > Save As.
- Save to
C:Program FilesGitcmd
- For File Name, type
.bashrc
. - To the right or “Save as Type” click on “Text Documents (*.txt)” and select All files (*.*).This is so Windows does not automatically add “.txt” to the file name.
- Click Save.
Container for Git cloning
Git commands need a GitHub repository to work with. So you’re welcome to my git-utilities repo, which has some commands you may like.PROTIP: Setup a container directory to house (group together) repositories you clone from GitHub. This is because cloning creates only the repository name and not the user. Although the author can be found with agit remote -v
command, you may want a way to put several repos for the same folder together, or additional related files such as pdf’s and website links. - On Mac: Open a Terminal window
On Windows, click on the Windows or Search icon, then type Po and right-click “Windows PowerShell” to select “Run as Admistrator. Click Yes to allow. - Begin from your user account’s home page. On Mac or Windows PowerShell:cd ~
- Create a folder to house your development projects:mkdir gits && cd gitsPROTIP: Instead of
gits
, some usedev
orSites
orProjects
to house related software development work, separate from other folders such as “Desktop” and “Document” under your MacOS user account folder.Here you can put various files related to Git, such as tutorial PDFs. However, some prefer to put such files in the.git
folder that the Git client installer creates under your user home folder. That is a different folder than the .git folder created for each repository cloned. - PROTIP: Create a folder representing the GitHub account to house new websites to be created (substituting “wilsonmar” with your GitHub user name):mkdir wilsonmar
- Only on MacOS, set permissions to write to the new folder. The $USER subsitute your own user name:sudo chown -R $USER wilsonmar
sudo chmod -R +rwx wilsonmarType in your password when prompted. - Navigate into the containing folder where a new directory will be automatically built by git clone commands:cd wilsonmar
Fork and/or Clone git-utilities
- Install a Git client if you have not already.
- Open another Terminal instance.
- Clone:git clone https://github.com/wilsonmar/git-utilities --depth=1The
depth=1
argument specifies to obtain only the latest commits for each object, thus not obtaining prior history.Alternately, if you intend on making changes, on GitHub Fork the repo to your own account, then clone the repo under your own account. - Navigate into the new repo (type cd and press Tab for auto-complete):cd git-utilities
Try aliases
Try the aliases defined above for MacOS and Windows:
- Instead of typing out
git status
, type:gsThis invokes the alias defined:If there has been no changes, the output is: - Instead of typing out
git branch -avv
, type:gbThis invokes the alias defined:A sample response:[Return to git-flow] - Instead of typing out
git log
, type:glThis invokes the alias defined:A sample response:[Return to git-flow] - Instead of typing out
git add . -A;git commit -m'Update';git push
, type:gbsThis invokes the alias defined:[Return to git-flow] - Instead of typing out
git add
andgit commit
for a single commit, type:This invokes the alias defined:PROTIP: Time saved using this can be huge because this reduces the “friction” to make small incremental changes.[Return to git-flow] - Instead of typing out
git fetch upstream
andgit checkout master
, type:This invokes the alias defined:PROTIP: Time saved using this can be huge because this reduces the “friction” to make small incremental changes.[Return to git-flow]Backup!
- When you’re done, save the file again and exit the program using the command expected by the editor you’re using.For Nano, press control + X to exit the program.
- PROTIP: So that you can recover quickly in case your laptop is no longer available, copy the
~/.bash_profile
file to an off-site backup location such as drive.google.com, in a folder called “mac-setup” or whatever you prefer.
Windows Doskey
The Windows equivalant of the
alias
command on Mac is:doskey macroName=macroDefinition
Windows Macro parameters are referenced in the definition via $ prefixed positions: $1 through $9 and $* for all.
set “cdMe=cd some_path”
Usage (from command line or script)
%cdMe%
See https://superuser.com/questions/560519/how-to-set-an-alias-in-windows-command-line
https://technet.microsoft.com/en-us/library/bb490894.aspx
PROTIP: Many Windows users are limited to save files only in their own user folder. So we’ll go with that limitation.
- Navigatecd C:Users%USERNAME%
- Create:mkdir gits
.bashrc file
More
This is one of a series on Git and GitHub:
Please enable JavaScript to view the comments powered by Disqus.